Commit a956a06
fix(kernel-agents): use external json parsing (#853)
## Summary
- Replace the bracket-counting heuristic in `sample-collector.ts` with
the
[`partial-json`](https://github.com/promplate/partial-json-parser-js)
library to correctly distinguish incomplete-but-valid JSON from
malformed JSON during LLM streaming
- Fix false positives when streamed JSON contains brace characters
inside string values (e.g. `{"think": "add a '}' here"}`)
- Use `Allow.OBJ` to reject non-object partial JSON early
- Wrap all `partial-json` errors in `SampleGenerationError` for
consistent error handling
Closes #673
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes core streaming-response parsing behavior, which can alter
when/why `SampleGenerationError` is thrown and may affect downstream
agent flows; impact is contained to JSON strategy parsing and is
test-covered.
>
> **Overview**
> Replaces the `makeSampleCollector` streaming JSON parsing heuristic
(brace counting + `JSON.parse`) with `partial-json` to distinguish
**complete**, **incomplete**, and **malformed** buffers during LLM
streaming, and to avoid false failures when braces appear inside string
values.
>
> Adds `partial-json` as a dependency and updates tests to reflect the
new error behavior (earlier rejection of non-JSON text, adjusted
max-chunk test input, and new coverage for braces-in-string handling).
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
418cc32. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>1 parent f7b4257 commit a956a06
4 files changed
Lines changed: 61 additions & 34 deletions
File tree
- packages/kernel-agents
- src/strategies/json
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
187 | 187 | | |
188 | 188 | | |
189 | 189 | | |
| 190 | + | |
190 | 191 | | |
191 | 192 | | |
192 | 193 | | |
Lines changed: 30 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
| 51 | + | |
53 | 52 | | |
54 | | - | |
| 53 | + | |
55 | 54 | | |
56 | 55 | | |
57 | 56 | | |
58 | | - | |
| 57 | + | |
59 | 58 | | |
60 | 59 | | |
61 | 60 | | |
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
65 | 64 | | |
66 | | - | |
67 | | - | |
68 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
77 | 100 | | |
Lines changed: 22 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
8 | 14 | | |
9 | 15 | | |
10 | 16 | | |
| |||
24 | 30 | | |
25 | 31 | | |
26 | 32 | | |
27 | | - | |
28 | | - | |
29 | 33 | | |
30 | 34 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
53 | 49 | | |
54 | 50 | | |
55 | | - | |
56 | | - | |
| 51 | + | |
57 | 52 | | |
58 | 53 | | |
59 | 54 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3674 | 3674 | | |
3675 | 3675 | | |
3676 | 3676 | | |
| 3677 | + | |
3677 | 3678 | | |
3678 | 3679 | | |
3679 | 3680 | | |
| |||
12398 | 12399 | | |
12399 | 12400 | | |
12400 | 12401 | | |
| 12402 | + | |
| 12403 | + | |
| 12404 | + | |
| 12405 | + | |
| 12406 | + | |
| 12407 | + | |
| 12408 | + | |
12401 | 12409 | | |
12402 | 12410 | | |
12403 | 12411 | | |
| |||
0 commit comments