-
Notifications
You must be signed in to change notification settings - Fork 0
[rig-sampler] fix: extract balanced JSON arrays from prose responses #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -422,6 +422,17 @@ describe("agent invocation", () => { | |
| await expect(reviewer("go")).resolves.toEqual({ text: "hello" }); | ||
| }); | ||
|
|
||
| it("parses a JSON array embedded in surrounding text", async () => { | ||
| mocks.setSendAndWaitImpl(async () => 'Here are the results:\n["alpha","beta"]\nDone.'); | ||
|
|
||
| const lister = agent({ | ||
| name: "lister", | ||
| output: s.array(s.string), | ||
| }); | ||
|
|
||
| await expect(lister("go")).resolves.toEqual(["alpha", "beta"]); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The new test covers the happy path but misses the case where prose contains 💡 Suggested regression testit("parses a JSON object when prose contains a bracket span before it", async () => {
mocks.setSendAndWaitImpl(async () => "Tags [optional]: {\"key\":\"value\"}");
const tagger = agent({
name: "tagger",
output: s.object({ key: s.string }),
});
await expect(tagger("go")).resolves.toEqual({ key: "value" });
});This test would currently fail, making the regression detectable before merging. Adding it alongside the implementation fix would lock in correctness for both types. |
||
| }); | ||
|
|
||
| it("retries validation failures with addon-customized repair prompts", async () => { | ||
| const prompts: string[] = []; | ||
| let calls = 0; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/diagnosing-bugs] Picking the earlier of
[and{can silently break object extraction when prose contains a bracket-delimited span (e.g."Use tags [optional]: {\"key\":1}") —[optional]would be extracted, fail JSON.parse, and return an error instead of finding the valid object.💡 Safer approach: try both, return the one that parses
One robust fix is to try extracting from both candidate positions and return whichever one successfully parses:
Alternatively, always prefer the earlier candidate but fall through to the other on parse failure — the current structure in
parseJsonalready catches parse errors, so the fallback could live there.The current heuristic works for the new test case but introduces a new failure mode for any prose that uses
[...]notation before a JSON object.