feat: add optimization for duration - #128
Closed
andrewklatzke wants to merge 1 commit into
Closed
Conversation
jsonbailey
approved these changes
Apr 9, 2026
3 tasks
andrewklatzke
added a commit
that referenced
this pull request
Jul 17, 2026
… release (#140) **Requirements** - [x] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [x] I have validated my changes against all supported platform versions **Related issues** This PR encapsulates all previous changes in the chain of optimization PRs that were broken up into smaller pieces. Consolidating here so that we can have a single commit/release of the package. The PRs were independently reviewed and approved. **Describe the solution you've provided** See: #116 #117 #119 #122 #127 #128 #130 #135 #139 <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **High Risk** > Large new surface area touching LaunchDarkly REST API writes (results, variations), API key handling, and complex multi-phase optimization state; regressions could affect live optimization runs or publish unintended variations when auto-commit is enabled. > > **Overview** > This PR delivers the **initial release** of the optimization package under the renamed PyPI distribution **`launchdarkly-ai-optimizer`** and import path **`ldai_optimizer`**, removing the old **`ldai_optimization`** placeholder (`ApiAgentOptimizationClient`). > > **`OptimizationClient`** is the new public entry point. Callers supply **`handle_agent_call`** / optional **`handle_judge_call`** so all LLM traffic stays in app code; the library runs the loop: agent turns, parallel judges (LaunchDarkly config judges or inline acceptance statements), optional **validation** on extra random samples, **LLM-driven variation generation** when attempts fail, and optional **Phase 2** cost/latency tuning after a quality win. > > Three entry modes are added: **`optimize_from_options`** (local options, optional **`auto_commit`**), **`optimize_from_ground_truth_options`** (all labeled samples must pass per attempt), and **`optimize_from_config`** (loads agent optimization config from the REST API, streams iteration results via POST/PATCH, preflight write check, default auto-commit). Supporting pieces include **`LDApiClient`**, option/dataclass types, prompts/utilities (retries, cost estimation, slug keys), expanded README, and **`PROVENANCE.md`** for wheel attestation verification. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit e8ec8d1. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requirements
Describe the solution you've provided
Adds additional instructions and data so that the variation generation can optimize for speed when prompted to. Uses a regex of various words ("speed" "snappy" etc.) that inform the prompt whether or not to do the duration optimization.
Describe alternatives you've considered
This could be added as a top-level option to the prompt (to optimize this specific dimension) but I think that would quickly explode into a ton of options. Instead I'm just relying on the users' intent with the acceptance statement to determine whether or not to include the instructions.
Duration tolerance might be tweaked in the future/made an option, but for now this achieves the request in the PRD.
Additional context
Example (abridged) output when including "make it faster" in the prompt:
Note
Medium Risk
Adds a new latency-based pass/fail gate and injects timing context into judge/variation prompts, which can change when optimizations succeed and may increase retries/iterations when timing data is present.
Overview
Adds duration-aware optimization that activates when any judge
acceptance_statementcontains latency keywords (via new_acceptance_criteria_implies_duration_optimization).When active, the client now (1) passes
agent_duration_msinto acceptance-statement judges so they can reference current vs baseline latency in their rationale, (2) includes per-iterationduration_msin variation-generation prompt history plus an extra Duration Optimization section, and (3) enforces a new duration gate (_evaluate_duration) requiring candidates to be <0.80 * baseline(history[0]) in chaos, validation, and ground-truth sample evaluation.Adds extensive tests covering keyword detection, judge instruction injection, duration gating edge cases, and end-to-end wiring in both chaos and ground-truth modes.
Reviewed by Cursor Bugbot for commit 5d76276. Bugbot is set up for automated code reviews on this repo. Configure here.