Repository navigation
fix(optimization): give JsonField numbers and booleans typed keys - #599
Conversation
8e4e0f4 to
f8555ba
Compare
|
Assessment: Comment The change does what it sets out to do. Numbers and booleans in a Review Categories
The validation is clear and well documented, and the error messages tell the user what to do. |
|
Assessment: Comment Re-reviewed Still open from the previous review
|
|
Assessment: Approve (from a code standpoint) Re-reviewed Remaining non-code items
Thanks for turning the feedback around quickly. |
A JsonField on a number or a boolean was a text key holding the field's JSON text. The factory received '0.7' instead of 0.7, the default search gave the key to Reflection rather than TPE, the score model treated it as text, and nothing bounded it: an integer field accepted "2.0", "2.5", "-1" and "1e9", and "2.0" was written into the file as 2.0. A number now takes the Int or Float to search it with, as JsonField's third argument, e.g. JsonField(cfg, "/temperature", Float(0.0, 1.0)). The file's value fills the marker's default, so a Float also takes an integer in the file. A boolean becomes OneOf(False, True), and a OneOf may also be given for a string, number, boolean, or null field. The candidate stores the typed value, so Space.validate's strict type and bound checks apply, TPE proposes the key, the factory receives the number or boolean, and the copy writes a JSON number or boolean. Strings, arrays, objects, and null without a marker are unchanged. A number without a marker now raises TypeError at declaration, as does a marker that has a default, doesn't fit the file's value, or is given for an array or object. Because a diff hunk can now hold only a number change, the summary counts "files or text values" in diff().
A JsonChoice subclasses OneOf, so JsonField accepted it, but its options
are canonical JSON text: the copy wrote that text into the file as a
string ("model": "\"small\"", "t": "0.7"), even for the baseline, and
the factory received the text. A JsonField's marker applies only to a
string, number, boolean, or null field, which a OneOf of the field's
values covers, so a JsonChoice now raises TypeError at declaration.
…eger in diff()
write() keeps {"top_p": 1} as 1 in the copy when a Float key equals it,
but _render, which diff() uses, always set the typed value, so the
review diff showed "top_p": 1.0, a line in neither the file nor the
copy. _render now leaves a field that already holds the value, as
write() does.
The copy test now compares the whole document, so it also catches a
rewrite that drops or changes the untouched fields, the big integers
included.
A OneOf on a string field may also list numbers, booleans, or None, e.g.
OneOf("small", 3, None). _holds checked the text case first, so it said
a file holding 3 or null didn't hold 3 or None, and write() rewrote a
file that already had the value. The marker is now checked first.
a36e23b to
848af80
Compare
Reading a factory's markers: a JsonField with an Int or Float is a number, with a OneOf or on a boolean a choice, otherwise a text. The Reflection warning covers only a text JsonField. Exit code 2 names a factory whose JsonField fails at declaration, and the deliverables say a changed JsonField is a hunk on its JSON file in best.diff.
|
Assessment: Approve (from a code standpoint) Re-reviewed
The optimization tests pass on the rebased branch (1862 passed, 2 skipped), and Remaining non-code items (unchanged)
|
|
Assessment: Approve (from a code standpoint) Re-reviewed
Remaining non-code items (unchanged)
|
Description
A
JsonFieldon a number or a boolean was a text key whose value was the field's JSON text. Take{"temperature": 0.7, "max_turns": 2}inagent.jsonand@tunable(temperature=JsonField(cfg, "/temperature"), max_turns=JsonField(cfg, "/max_turns")). Then:'0.7'and'2', both in the plain call and frombind();text, so the default search gave them toReflectioninstead ofTPE, and the score model left them out of its numeric factors;max_turnsaccepted"2.0","2.5","-1"and"1e9", and"2.0"wrote"max_turns": 2.0into the candidate's copy of the file.This PR gives such a field a typed key:
IntorFloatto search it with, as a third argument:JsonField(cfg, "/temperature", Float(0.0, 1.0))orJsonField(cfg, "/max_turns", Int(1, 20)). The file's value is the baseline, so the marker is passed withoutdefault=. AFloatalso takes an integer in the file, so{"top_p": 1}gives a baseline of1.0; the baseline's copy keeps the file's bytes, and while the value is unchanged both the copy anddiff()keep the file's1.OneOf(False, True)key.OneOfmay also be given for a string, number, boolean, or null field, to search it among those options, e.g.JsonField(cfg, "/model", OneOf("small", "large")).The candidate stores the typed value.
Space.validatethen applies its usual strict checks (anintkey rejects2.0and"2", and bounds apply), the default search puts these keys in the "numbers" block withTPE, the factory receives0.7,2, orTrue, and the copy writes them as JSON numbers and booleans.Space.create_candidate_copyand the diff renderer pass aJsonFieldits value as is instead ofstr(value), which would turnTrueinto"True".A changed JSON number shows in its file's hunk in
diff()and inchanged_values().Behavior changes (
JsonFieldhas not been released yet):JsonFieldon a number without a marker raisesTypeErrorat declaration and asks for anIntorFloat. Before, it declared an unbounded text key.TypeErrorwhen it has adefault=, doesn't fit the file's value, isn't anInt,Float, orOneOf, is aJsonChoice(its options are JSON text, which would be written into the file as strings), or is given for an array or object. AFloatover an integer beyond2**53, where a float stops holding every integer, raises too. So doessize_caporedit_instructionswith a marker or on a boolean, since nothing reads them there.{"max_turns": "3"}passed tobind()or proposed by a custom proposer fails validation.Block(Reflection(), "max_turns")on a number or booleanJsonFieldnow raises atSearch.resolve, as it does for anIntkey.JsonFieldwon't resume after it, because the space recorded inrun.jsondiffers.Related Issues
Part of #442. Fixes behavior added in #504, which introduced
JsonField.Documentation PR
None. The optimize skill (
.agents/skills/optimize/SKILL.md) is updated in this PR: how to read a typedJsonFieldin a factory, the Reflection warning for a textJsonFieldonly, the declaration errors, and where a changedJsonFieldshows inbest.diffandbest.values.json.Type of Change
Bug fix
Testing
New tests in
tests/strands_evals/optimization/declaration/test_files.pycoverint,float, and boolean keys, aFloatover an integer in the file,OneOfon a string and on null, the rejection of"2.0","2",2.0,2.5,True,-1, and10**9for anintkey, the declaration errors, what the factory receives from the plain call and frombind(), the baseline copy keeping the file's bytes, and how a changed copy writes each typed key.test_blocks.pychecks the default blocks, andtest_candidate.pythe diff (including aFloatover a file's integer) andchanged_values().Tests that used an unbounded number
JsonFieldnow pass anIntor use an array or object instead. The 31 new typed-key cases, and the 3 cases moved toInt(1, 20)fail on the base branch and pass here. The 7 cases moved to arrays and objects pass on both.Rebased onto
main:hatch test tests/strands_evals/optimization/ tests/strands_evals/cli/gives 2575 passed and 1 skipped;hatch fmt --linter(ruff and mypy) andhatch fmt --formatter --checkpass.I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.