Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@
- Progress bars per dataset
- Tests for CLI integration with multiple datasets

## M81: Automatic Threshold Tuning
## M81: Automatic Threshold Tuning ✅
- `xpyd-acc auto-threshold --reports r1.json r2.json ...` analyzes historical batch reports
- Suggests optimal `--fail-threshold` based on observed divergence rate distribution
- Suggests optimal `--numeric-tolerance` based on logprob gap distribution at divergence points
Expand All @@ -774,3 +774,15 @@
- Rich terminal output with distribution summary and recommended values
- `auto_threshold.py` module: `analyze_thresholds()`, `ThresholdRecommendation`, `format_recommendations()`
- 12 tests covering threshold computation, edge cases, formatting, JSON export, CLI integration

## M82: Interactive REPL for Exploratory Comparison
- `xpyd-acc repl --baseline <url> --target <url> --model <model>` launches interactive shell
- Type a prompt → sends to both endpoints → shows side-by-side output comparison
- Built-in REPL commands: `:logprobs` (toggle logprob display), `:diff` (show token diff of last result)
- `:history` shows previous prompts and results in session
- `:set temperature=0.5` to adjust sampling params on the fly
- `:export <path>` saves session history as JSON
- `--api-key` flag, env var, and TOML config supported
- Readline-style line editing and history (via stdlib `readline`)
- `repl.py` module: `run_repl()`, `ReplSession`, `ReplCommand`
- 12 tests covering session state, command parsing, export, edge cases, CLI integration
65 changes: 29 additions & 36 deletions docs/iterations/current.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
# Current Iteration — M81
# Current Iteration — M82

## Milestone

**M81** — Automatic Threshold Tuning
**M82** — Interactive REPL for Exploratory Comparison

## What Was Done

Added `xpyd-acc auto-threshold --reports r1.json r2.json ...` command that analyzes
historical batch reports and recommends optimal `--fail-threshold` and `--numeric-tolerance`
values based on percentile-based statistical analysis.
Added `xpyd-acc repl --baseline <url> --target <url> --model <model>` — an interactive
shell for exploratory comparison of two endpoints.

### Implementation

- `auto_threshold.py` module:
- `ThresholdRecommendation` dataclass with `to_dict()` serialization
- `analyze_thresholds()` — percentile-based recommendation engine
- `load_reports()` — fault-tolerant multi-report loader
- `format_recommendations()` — terminal display
- `_percentile()` — interpolating percentile computation
- Fail threshold: p95 of observed divergence rates × 1.1 + headroom
- Numeric tolerance: p95 of logprob gaps at divergence points
- Confidence scoring: high (≥500 samples, ≥3 reports), medium, low
- `--percentile <float>` flag to control recommendation aggressiveness
- `--json <path>` export

### Files Changed

- `src/xpyd_acc/auto_threshold.py` (new)
- `src/xpyd_acc/cli/report.py` — added `_run_auto_threshold()` handler
- `src/xpyd_acc/cli/parsers.py` — registered `auto-threshold` subcommand
- `src/xpyd_acc/cli/__init__.py` — wired handler to early dispatch
- `tests/test_auto_threshold.py` (new, 18 tests)
- `docs/iterations/current.md` — updated

### Tests

18 tests covering:
- Percentile computation (basic, p95, empty, single)
- Threshold analysis (empty reports, single report, multiple/high-confidence, no logprob gaps, custom percentile)
- ThresholdRecommendation serialization and JSON round-trip
- Format output with data and without data
- Report loading (valid, missing, mixed)
- CLI integration (basic and custom percentile)
- `repl.py` module:
- `ReplSession` dataclass: tracks state, params, history
- `ReplCommand` dataclass: parsed REPL command
- `ReplEntry` dataclass: single comparison result
- `run_repl()` async function: main REPL loop with injectable I/O
- `run_repl_iteration()`: sends prompt to both endpoints concurrently
- `parse_command()`: parses `:command args` syntax
- `format_comparison()`, `format_diff()`, `format_history()`: display helpers
- `send_prompt()`: async HTTP request to OpenAI-compatible endpoint
- REPL commands: `:logprobs`, `:diff`, `:history`, `:set key=value`, `:export <path>`, `:quit`, `:help`
- `ReplSession.set_param()` for live parameter adjustment (temperature, top_p, seed, max_tokens)
- `ReplSession.export_json()` for session history export
- CLI: `xpyd-acc repl` subcommand with `--baseline`, `--target`, `--model`, `--api-key` flags
- CLI wired via `cli/parsers.py` → `cli/report.py` → `cli/__init__.py`

### Tests (32 tests)

- `TestParseCommand`: command parsing, args, empty colon, case insensitivity
- `TestReplSession`: set_param for all types, unknown param, export_json
- `TestFormatFunctions`: comparison match/diverge, diff, history empty/with entries
- `TestReplLoop`: quit, help, set, logprobs toggle, diff no history, history, export,
EOF exit, unknown command, empty input, set without equals, export without path
- `TestCLIIntegration`: parser registration

## Iteration History

Expand All @@ -54,4 +46,5 @@ values based on percentile-based statistical analysis.
| M78 | 2026-04-06 | Grafana Dashboard Template Export | ✅ merged | Both approved |
| M79 | 2026-04-06 | Parallel Multi-Dataset Batch Run | ✅ merged | Both approved |
| M80 | 2026-04-06 | Multi-Dataset CLI Integration | ✅ merged | Both approved |
| M81 | 2026-04-06 | Automatic Threshold Tuning | ⏳ pending review | — |
| M81 | 2026-04-06 | Automatic Threshold Tuning | ✅ merged | Both approved |
| M82 | 2026-04-06 | Interactive REPL for Exploratory Comparison | ⏳ pending review | — |
2 changes: 2 additions & 0 deletions src/xpyd_acc/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
_run_grafana_dashboard,
_run_prometheus,
_run_regression,
_run_repl,
_run_report,
_run_summary,
)
Expand Down Expand Up @@ -131,6 +132,7 @@ def main(argv: list[str] | None = None) -> None:
"profiles": lambda: _run_profiles(config),
"completion": lambda: _run_completion(args, parser),
"auto-threshold": lambda: _run_auto_threshold(args),
"repl": lambda: _run_repl(args),
}

if args.command in _early:
Expand Down
12 changes: 12 additions & 0 deletions src/xpyd_acc/cli/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def register_all(sub: argparse._SubParsersAction) -> None:
_register_prometheus(sub)
_register_grafana_dashboard(sub)
_register_auto_threshold(sub)
_register_repl(sub)
def _register_compare(sub):
lp = sub.add_parser("compare-logprobs", help="Compare logprobs between two endpoints")
lp.add_argument("--baseline", required=True, help="Baseline endpoint URL")
Expand Down Expand Up @@ -594,3 +595,14 @@ def _register_auto_threshold(sub):
"--json", default=None,
help="Export recommendations as JSON to this path",
)


def _register_repl(sub):
rp = sub.add_parser(
"repl",
help="Interactive REPL for exploratory comparison",
)
rp.add_argument("--baseline", required=True, help="Baseline endpoint URL")
rp.add_argument("--target", required=True, help="Target endpoint URL")
rp.add_argument("--model", default="default", help="Model name")
rp.add_argument("--api-key", default=None, help="API key")
14 changes: 14 additions & 0 deletions src/xpyd_acc/cli/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,17 @@ def _run_auto_threshold(args: argparse.Namespace) -> None:
json.dumps(rec.to_dict(), indent=2), encoding="utf-8"
)
print(f"\nRecommendations exported to {args.json}")


def _run_repl(args: argparse.Namespace) -> None:
"""Launch interactive REPL for exploratory comparison."""
import asyncio

from xpyd_acc.repl import run_repl

asyncio.run(run_repl(
baseline_url=args.baseline,
target_url=args.target,
model=args.model,
api_key=getattr(args, "api_key", None),
))
Loading
Loading