Skip to content

fix(optimization): compact result.json with transcripts referenced by hash - #610

Merged
nhungbi merged 1 commit into
strands-agents:mainfrom
jjbuck:opt/result-json-compact
Nov 25, 2026
Merged

nhungbi merged 1 commit into
strands-agents:mainfrom
jjbuck:opt/result-json-compact

Conversation

@jjbuck

@jjbuck jjbuck commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Description

<run>/result.json holds the whole OptimizationResult. Two things make it much larger than the data it carries:

  • The row of result.reports[candidate][split].cases for each completed trial is its EvaluationData dump, and its actual_trajectory inlines the trial's whole trajectory (a Session of spans, or the list a rollout returned). There is one row per evaluator per trial, so each trajectory is written once per evaluator. The run directory already stores each trajectory once in transcripts/<hash>.json, and the case rows (result.to_rows("case")) already name that file in their transcript column.
  • save_result and OptimizationResult.to_file pretty-print with indent=2, which roughly doubles the size of the nested span data.

On a run of a real Agent with a scripted model (1,728 trials, one evaluator, 10 iterations), result.json was 7.4 MB. Written compact it would be 3.7 MB, of which 1.75 MB is the inlined Session trajectories. Longer conversations and more evaluators make the inlined copies a larger share of the file.

This PR:

  • builds each report row with actual_trajectory=None and adds a transcript tag beside evaluator, evaluator_type and run_index. The tag holds the same hash as the case row for that trial, or None when the trial record holds no trajectory. Both views get the hash from the same helper, so they always agree;
  • gives both views a hash only when the record they show holds a trajectory. Before this, a case row could name a transcript that its trial record does not hold. After a resume, the last trial event for a trial can be a killed iteration's successful re-run, while the record the resumed run keeps is the failed one from the closed iterations;
  • writes result.json (RunDirectory.save_result) and OptimizationResult.to_file as compact JSON. run.json, effects.json and the .values.json files stay indented, since they are small and meant to be read by people.

The same run's result.json is now 2.0 MB. What remains grows with the number of report rows (trials times evaluators), not with the length of the trajectories. Each row still repeats the case input, the output, and any interactions and environment state; this PR does not change that.

Behavior changes (the optimizer has not been released yet; it is only on workstream/optimizer-workflow):

  • result.reports no longer carries trajectories, either in the returned result or after from_file. A trajectory is read from run_dir / "transcripts" / f"{row['transcript']}.json", so reading one needs the run folder. EvaluationReport.display(include_actual_trajectory=True) on these reports shows None.
  • Every report row has a new transcript key.
  • result.json and to_file output are a single line of compact JSON.

OptimizationResult.from_file needs no change. It loads the new files, still loads a result written before this change (indented, trajectories inline), and still needs no run folder.

Related Issues

Part of #442. The result and its reports were added in #535.

Documentation PR

None

Type of Change

Bug fix

Testing

  • New test_the_result_names_each_trajectory_by_its_transcript_hash_and_is_written_compact runs with two evaluators and two runs per case, with a rollout that returns a distinct list trajectory per trial, raises on one case and returns no trajectory on another. It checks that result.json holds no trajectory text and is compact, that to_file writes the same bytes, that every report row has no inline trajectory and carries the case row's hash (None for the crashed and the bare case), that each named file holds that trial's trajectory, that from_file gives back an equal result, and that an indented result with inline trajectories still loads.

  • New test_a_finished_run_resumed_rewrites_the_same_transcript_hashes checks that resuming the finished run rewrites result.json with the same reports.

  • New test_a_failed_record_kept_on_resume_names_no_transcript_of_a_killed_rerun kills a run after a step re-ran a candidate's failed trials and resumes it with a different proposal. That candidate's failed rows name no transcript in either view.

  • New smoke scenario test_the_result_names_each_session_transcript_by_its_hash runs a real Agent on a scripted model and checks that no Session is inlined in result.json and that each report row's transcript file holds that trial's Session.

  • test_reports_match_what_experiment_returns_for_the_same_trials now also drops the new transcript tag before comparing with Experiment's rows, and checks that the tag is None when the rollout saves no trajectory.

  • test_effects_diffs_and_the_result_are_written now checks the exact compact text save_result writes.

  • All of the above fail on the base branch.

  • pytest tests/strands_evals/optimization tests/strands_evals/types/optimization gives 1,460 passed and 1 xfailed (1,456 passed and 1 xfailed on the base branch).

  • ruff check and ruff format --check on the changed files pass. mypy -p strands_evals.optimization reports no errors in the optimization modules; the 23 errors it reports elsewhere are the same on the base branch.

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@jjbuck
jjbuck requested a review from a team as a code owner November 17, 2026 21:41
@jjbuck
jjbuck requested a review from opieter-aws November 17, 2026 21:41
@github-actions github-actions Bot added area-core Core eval framework: Case, Experiment, task handler, evaluation data stores bug Something isn't working labels Nov 17, 2026
@nhungbi

nhungbi commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

This change shouldn't affect search behaviors but the evaluation reports no longer hold actual trajectory. Users also need the transcript folder to be able to see them. Is this a valid trade-off?

@nhungbi
nhungbi force-pushed the opt/result-json-compact branch from 360a182 to 7a5ab1b Compare November 24, 2026 21:41
@nhungbi
nhungbi changed the base branch from workstream/optimizer-workflow to main November 24, 2026 21:41
@nhungbi
nhungbi force-pushed the opt/result-json-compact branch from 7a5ab1b to bb7e047 Compare November 24, 2026 22:53
@nhungbi

nhungbi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Rebased onto main after #606 and #608 merged (one commit, bb7e047), and the base changed from workstream/optimizer-workflow to main. Changes since the description was written:

Hashes come from each record's own transcript. #608 keeps a pointer ({hash, format}) per kept trial record (Optimizer._transcripts), so the case rows and the report rows now take the hash from it. This replaces reading the last trial event per key and checking record.trajectory is None: _transcripts(events) and _record_transcript are gone. The resume case this PR fixed (a killed iteration's re-run recorded after the failed record that is kept) still names no transcript; the pointer comes from the closed iterations' trial. The reports no longer read any trajectory back from disk at the end of the run.

New public method: OptimizationResult.load_transcript(transcript, *, run_dir=None). Since #608, a Session transcript file is stored packed (each message once), so run_dir / "transcripts" / f"{hash}.json" can't be parsed as a Session directly, and a row holds only the hash. result.load_transcript(row["transcript"]) takes a report row's or a case row's hash and returns the Session (packed, or a whole dump as runs before #608 saved it), the list a rollout returned, or None when the trial kept no trajectory. The format is told by the file's shape, as the UI does since #608. run_dir= reads a run folder that was moved; a missing file raises FileNotFoundError. The reports and run_dir docstrings point to it instead of the file path.

Behavior changes: as described, except that the optimizer is now on main (still unreleased), and a trajectory is read with load_transcript rather than from the file.

Tests:

  • test_the_result_names_each_trajectory_by_its_transcript_hash_and_is_written_compact also checks that each row's hash is its kept record's transcript, reads each through load_transcript, and covers None and a moved run folder (FileNotFoundError at the old path, read with run_dir=).
  • fix(optimization): store each transcript message once and drop trajectories from the optimizer's trial records #608's test_trial_records_drop_their_trajectories_and_proposers_read_the_parents_back now checks that the report rows hold no trajectory and name their transcripts, that each reads back as the trial's session, and that a whole Session dump still loads.
  • test_the_result_names_each_session_transcript_by_its_hash (smoke) reads each row's session through load_transcript.
  • test_a_failed_record_kept_on_resume_names_no_transcript_of_a_killed_rerun removes the killed step's draw event before resuming. Since fix(optimization): replay a killed iteration's proposals on resume #616 a resumed step replays the killed one's proposals, which re-proposes the same candidate and hides the case; without the event the step asks its proposers again, as a resumed step wider than the killed one does.
  • pytest tests/strands_evals/optimization tests/strands_evals/types/optimization tests/strands_evals/cli tests/strands_evals/telemetry: 2627 passed, 1 skipped. Lint, mypy and format pass.

liramon2
liramon2 previously approved these changes Nov 24, 2026
… hash

result.json held every trial's trajectory again: each row of
OptimizationResult.reports inlined the trial's Session (or list) in
actual_trajectory, once per evaluator, although the run directory already
stores each trajectory once in transcripts/<hash>.json. It was also
indented, which roughly doubles the size of nested span data. On a
1,728-trial scripted run the file was 7.4 MB; it is now 2.0 MB.

Report rows now have actual_trajectory=None and a transcript tag holding
the same hash as the case rows, read through one helper so the two views
agree. Both views name a transcript only when the trial record they show
holds a trajectory: after a resume, a trial's last event can be a killed
iteration's successful re-run while the record kept is the failed one.

save_result and OptimizationResult.to_file write compact JSON. from_file
needs no change; results written before this change still load.
@github-actions

Copy link
Copy Markdown

Assessment: Comment

This review covers head bb7e047, after the rebase onto main. Storing each trajectory once and having rows point to it by hash is a good change. The hash for both the case rows and the report rows now comes from each record's own {hash, format} pointer (#608), so the resume case this PR fixes holds up. Nothing blocks the merge; the points below are about the new public surface.

Review Categories
  • API review: The PR adds a public method, OptimizationResult.load_transcript(transcript, *, run_dir=None). It also changes the contract of result.reports: no inline actual_trajectory, and a new transcript key on every row. The PR has no api/needs-review label. The optimizer is unreleased, but these shapes become the documented way to read a trajectory, so they're worth a quick API pass. That pass is also the right place to settle @nhungbi's trade-off question, since reports now need the run folder to show trajectories. One option is a small convenience for users who want self-contained reports, such as an include_trajectories= flag or helper that rehydrates a report through load_transcript.
  • Duplicated logic: Transcript decoding now lives in two places with different dispatch rules; see the inline comment.
  • Output format: Making to_file compact is reasonable for result.json, but people who call it directly lose readable output with no way to get it back. An optional indent: int | None = None keyword would keep the compact default and still give them a choice.
  • Verification: I couldn't run the suite in this environment (pytest isn't installed), so this review is from reading the code only.

The new tests are thorough, especially the resume and moved-folder cases.

@github-actions

Copy link
Copy Markdown

Re-review of cd04d34: Assessment unchanged (Comment).

Compared with bb7e047, the only file that differs is tests_integ/test_optimize_typescript_agent.py. It now reads each session through result.load_transcript(case["transcript"]), which fits the new row shape and looks right. Nothing changed in src/, so the earlier inline comment about the duplicated transcript decoding and the summary points (API review label, an optional indent for to_file) still apply. Neither has a reply yet.

@github-actions

Copy link
Copy Markdown

Re-review of 0dd05f7: Assessment unchanged (Comment), and nothing blocks the merge.

The duplicated-decoding comment is fixed: both readers now call one read_transcript helper, which also checks the hash so load_transcript can't be pointed at a file outside transcripts/. The tests cover reading by format and by shape, as well as bad hashes. Two non-blocking points from the first summary are still open:

  • An api/needs-review pass for the new OptimizationResult.load_transcript and the changed reports row shape. That pass can also settle the trade-off @nhungbi raised.
  • An optional indent keyword on to_file, for people who want readable output.

As before, I reviewed this by reading the code; I couldn't run tests or lint here.

@nhungbi
nhungbi merged commit 707bbf3 into strands-agents:main Nov 25, 2026
20 checks passed

This branch was successfully deployed

1 active deployment
auto-approve — 0dd05f70 Deployed Nov 24, 2026 by nhungbi via Trigger Strands Review #967
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-core Core eval framework: Case, Experiment, task handler, evaluation data stores bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants