Skip to content

feat: add optimization for duration - #128

Closed
andrewklatzke wants to merge 1 commit into
aklatzke/AIC-2118/add-additional-validation-to-chaos-modefrom
aklatzke/AIC-2154/optimize-for-duration
Closed

feat: add optimization for duration#128
andrewklatzke wants to merge 1 commit into
aklatzke/AIC-2118/add-additional-validation-to-chaos-modefrom
aklatzke/AIC-2154/optimize-for-duration

Conversation

@andrewklatzke

@andrewklatzke andrewklatzke commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

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:

      "rationale": "... Latency was 2410ms, which is 2515ms faster than the 4925ms baseline, so it also meets the speed goal well.",


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_statement contains latency keywords (via new _acceptance_criteria_implies_duration_optimization).

When active, the client now (1) passes agent_duration_ms into acceptance-statement judges so they can reference current vs baseline latency in their rationale, (2) includes per-iteration duration_ms in 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.

@andrewklatzke
andrewklatzke requested a review from jsonbailey April 8, 2026 22:39
@andrewklatzke
andrewklatzke requested a review from a team as a code owner April 8, 2026 22:39
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 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants