/optimize
Minimal codex-native optimization loop. Use for metric-driven improvements with guardrails and measurable gates.
$ npx -y skills add Borda/AI-Rig --skill optimize --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.
- You can call itInvoke it directly when you want it.
- Slash command
/optimize
Context preview
The summary Claude sees to decide when to auto-load this skill.
Minimal codex-native optimization loop. Use for metric-driven improvements with guardrails and measurable gates.
SKILL.md
optimize.SKILL.mdname: optimize
description: Minimal codex-native optimization loop. Use for metric-driven improvements with guardrails and measurable gates.
Optimize
Metric-driven optimization with explicit guards, rollback criteria, experiment log.
Input Schema
{
"goal": "required measurable improvement objective",
"mode": "single|campaign",
"metric_cmd": "required command that emits or validates the target metric",
"metric_direction": "higher|lower",
"guard_cmd": "required command that must continue to pass",
"max_iterations": "optional integer, default 1",
"min_delta": "optional practical significance threshold",
"scope_files": [
"paths the optimization may edit"
],
"done_when": "metric improves without guard regression"
}Workflow
01: Create run directory
Run `python PLUGIN_ROOT/shared/create_run.py --skill optimize` once. Retain its single printed path as `<run-directory>` and substitute that literal path into every later artifact path and helper argument. Never store or reuse the path through a shell variable; shell variables do not persist across tool calls.
02: Validate metric and guard commands
Require:
- Repeatable `metric_cmd` producing comparable value or pass/fail.
- Known `metric_direction`.
- `guard_cmd` fails on unacceptable regressions.
- Bounded `scope_files`.
- Explicit, bounded `max_iterations` for `campaign`.
- Protect files/scripts used by `metric_cmd`/`guard_cmd` unless user explicitly scopes them and accepts measurement-integrity risk.
Dry-run both before edit:
Execute the configured `metric_cmd` and `guard_cmd` separately with the host-native command runner. Write complete combined output to `<run-directory>/metric-baseline.txt` and `<run-directory>/guard-baseline.txt`; retain both exit codes and stop before editing if either command cannot run.
03: Record baseline and hypothesis
Write `<run-directory>/hypothesis.md`:
- metric to improve
- expected mechanism
- files allowed to change
- guard risk
- rollback condition
For `campaign`, noisy metrics, GPU/ML performance, or correctness-sensitive code, apply `../../shared/specialist-orchestration.md`. Write `<run-directory>/specialist-optimization-plan.md` with narrow context packs for:
- `squeezer`: profiling mechanism, bottleneck hypothesis, measurement plan.
- `qa-specialist`: guard coverage and regression risk.
- `data-steward`: data pipeline or reproducibility impact.
- `scientist`: metric validity, ablation design, statistical noise.
- `challenger`: overfitting to the metric or weakening guard checks.
No fan-out for one small measured change with stable metric/guard. Never let specialist change metric/guard scripts unless explicitly in `scope_files` and measurement-integrity risk recorded.
**Structural context (optional)**: when `scope_files` resolves to a Python module/symbol, also probe codemap-py once for callers, coupling, and test impact before the first iteration: `python PLUGIN_ROOT/shared/codemap_adapter.py context --category develop --target <qname> --out <run-directory>/codemap-context.json`. Per `../../shared/codemap-contract.md`, absence/incompatibility is non-fatal — continue with the hypothesis above. Persist the result once here, before step 04 applies any change; any triggered specialist consumes `<run-directory>/codemap-context.json`, never a fresh query.
Initialize machine-readable iteration log:
Create an empty `<run-directory>/experiments.jsonl` with the filesystem tool before the first iteration.
04: Apply one minimal optimization change per iteration
One independent hypothesis per iteration. Do not optimize unmeasured paths. Before each, write `<run-directory>/iteration-<n>-before.patch` with scoped-file diff. If iteration fails and only its patch is present, revert with `git apply -R` against iteration diff; otherwise fail run when clean reversal cannot be proven. Never use `git reset --hard`.
05: Re-measure
Re-run the same retained `metric_cmd` and `guard_cmd` separately with the host-native command runner. Write complete combined output to `<run-directory>/metric-after.txt` and `<run-directory>/guard-after.txt`; retain both exit codes.
06: Compare baseline and after results in `<run-directory>/comparison.md`
Required fields:
- baseline value
- after value
- delta
- guard status
- confidence
- noise caveats
Append one JSON object/iteration to `<run-directory>/experiments.jsonl`:
{
"iteration": 1,
"hypothesis": "one-line mechanism",
"metric_before": 0.0,
"metric_after": 0.0,
"delta": 0.0,
"guard": "pass|fail",
"decision": "kept|reverted|inconclusive|failed",
"rollback_evidence": "path or reason"
}07: Decide keep/revert
- Keep only with intended metric movement and passing guards.
- With `min_delta`, keep only if delta meets/exceeds practical-significance threshold.
- Revert or fail on guard regression.
- For noisy measurement, repeat or mark inconclusive.
- In `campaign`, stop at first kept result unless user asked continued exploration; otherwise continue only while `max_iterations` remains and each rejected iteration has rollback evidence.
08: Run shared quality gates
Inspect `python PLUGIN_ROOT/shared/run_gates.py --help`. Tests runs configured test or guard command; give real commands or explicit reasons for other gates.
09: Write and validate the mandatory result artifact
Follow `../../shared/helper-cli-contract.md` and authoritative help. Write `OPTIMIZE_METADATA`, validate as `optimize`, promote only validated candidate.
Fail-Fast Rules
1. Missing metric or guard command => fail. 2. Baseline cannot be captured => fail. 3. Scope is unbounded => fail. 4. Guard regression after change => fail unless reverted. 5. Metric/guard script changed without explicit scope and measurement-integrity note => fail. 6. Campaign iteration rejected without rollback evidence or unresolved-risk note => fail. 7. Claimed improvement below `min_delta` without explicit
Read more
name: optimize description: Minimal codex-native optimization loop. Use for metric-driven improvements with guardrails and measurable gates.
Optimize
Metric-driven optimization with explicit guards, rollback criteria, experiment log.
Input Schema
{
"goal": "required measurable improvement objective",
"mode": "single|campaign",
"metric_cmd": "required command that emits or validates the target metric",
"metric_direction": "higher|lower",
"guard_cmd": "required command that must continue to pass",
"max_iterations": "optional integer, default 1",
"min_delta": "optional practical significance threshold",
"scope_files": [
"paths the optimization may edit"
],
"done_when": "metric improves without guard regression"
}Workflow
01: Create run directory
Run `python PLUGIN_ROOT/shared/create_run.py --skill optimize` once. Retain its single printed path as `<run-directory>` and substitute that literal path into every later artifact path and helper argument. Never store or reuse the path through a shell variable; shell variables do not persist across tool calls.
02: Validate metric and guard commands
Require:
- Repeatable `metric_cmd` producing comparable value or pass/fail.
- Known `metric_direction`.
- `guard_cmd` fails on unacceptable regressions.
- Bounded `scope_files`.
- Explicit, bounded `max_iterations` for `campaign`.
- Protect files/scripts used by `metric_cmd`/`guard_cmd` unless user explicitly scopes them and accepts measurement-integrity risk.
Dry-run both before edit:
Execute the configured `metric_cmd` and `guard_cmd` separately with the host-native command runner. Write complete combined output to `<run-directory>/metric-baseline.txt` and `<run-directory>/guard-baseline.txt`; retain both exit codes and stop before editing if either command cannot run.
03: Record baseline and hypothesis
Write `<run-directory>/hypothesis.md`:
- metric to improve
- expected mechanism
- files allowed to change
- guard risk
- rollback condition
For `campaign`, noisy metrics, GPU/ML performance, or correctness-sensitive code, apply `../../shared/specialist-orchestration.md`. Write `<run-directory>/specialist-optimization-plan.md` with narrow context packs for:
- `squeezer`: profiling mechanism, bottleneck hypothesis, measurement plan.
- `qa-specialist`: guard coverage and regression risk.
- `data-steward`: data pipeline or reproducibility impact.
- `scientist`: metric validity, ablation design, statistical noise.
- `challenger`: overfitting to the metric or weakening guard checks.
No fan-out for one small measured change with stable metric/guard. Never let specialist change metric/guard scripts unless explicitly in `scope_files` and measurement-integrity risk recorded.
**Structural context (optional)**: when `scope_files` resolves to a Python module/symbol, also probe codemap-py once for callers, coupling, and test impact before the first iteration: `python PLUGIN_ROOT/shared/codemap_adapter.py context --category develop --target <qname> --out <run-directory>/codemap-context.json`. Per `../../shared/codemap-contract.md`, absence/incompatibility is non-fatal — continue with the hypothesis above. Persist the result once here, before step 04 applies any change; any triggered specialist consumes `<run-directory>/codemap-context.json`, never a fresh query.
Initialize machine-readable iteration log:
Create an empty `<run-directory>/experiments.jsonl` with the filesystem tool before the first iteration.
04: Apply one minimal optimization change per iteration
One independent hypothesis per iteration. Do not optimize unmeasured paths. Before each, write `<run-directory>/iteration-<n>-before.patch` with scoped-file diff. If iteration fails and only its patch is present, revert with `git apply -R` against iteration diff; otherwise fail run when clean reversal cannot be proven. Never use `git reset --hard`.
05: Re-measure
Re-run the same retained `metric_cmd` and `guard_cmd` separately with the host-native command runner. Write complete combined output to `<run-directory>/metric-after.txt` and `<run-directory>/guard-after.txt`; retain both exit codes.
06: Compare baseline and after results in `<run-directory>/comparison.md`
Required fields:
- baseline value
- after value
- delta
- guard status
- confidence
- noise caveats
Append one JSON object/iteration to `<run-directory>/experiments.jsonl`:
{
"iteration": 1,
"hypothesis": "one-line mechanism",
"metric_before": 0.0,
"metric_after": 0.0,
"delta": 0.0,
"guard": "pass|fail",
"decision": "kept|reverted|inconclusive|failed",
"rollback_evidence": "path or reason"
}07: Decide keep/revert
- Keep only with intended metric movement and passing guards.
- With `min_delta`, keep only if delta meets/exceeds practical-significance threshold.
- Revert or fail on guard regression.
- For noisy measurement, repeat or mark inconclusive.
- In `campaign`, stop at first kept result unless user asked continued exploration; otherwise continue only while `max_iterations` remains and each rejected iteration has rollback evidence.
08: Run shared quality gates
Inspect `python PLUGIN_ROOT/shared/run_gates.py --help`. Tests runs configured test or guard command; give real commands or explicit reasons for other gates.
09: Write and validate the mandatory result artifact
Follow `../../shared/helper-cli-contract.md` and authoritative help. Write `OPTIMIZE_METADATA`, validate as `optimize`, promote only validated candidate.
Fail-Fast Rules
1. Missing metric or guard command => fail. 2. Baseline cannot be captured => fail. 3. Scope is unbounded => fail. 4. Guard regression after change => fail unless reverted. 5. Metric/guard script changed without explicit scope and measurement-integrity note => fail. 6. Campaign iteration rejected without rollback evidence or unresolved-risk note => fail. 7. Claimed improvement below `min_delta` without explicit
Showing the first part of this file.
Specialist-agent infrastructure for Python/ML OSS — the scaffolding that lets you maintain at scale without becoming a full-time reviewer.
Repo: Borda/AI-Rig
Other skills on ai-rig.
- /debug
Investigation-first debugging — gather evidence, form confirmed root-cause hypothesis, hand off to fix mode with diagnosis file. TRIGGER when: user reports a symptom or failing test with Python traceback, or asks to investigate a runtime/CI failure with reproducible evidence;
Open skill - /feature
TDD-first feature development — crystallise API as a demo test, drive implementation to pass it, run quality stack and progressive review loop. TRIGGER when: user asks to build new functionality, add a capability, or implement a feature in a Python project; phrases: \"add X\",
Open skill - /fix
Reproduce-first bug resolution — capture bug in failing regression test, apply minimal fix, run quality stack and review loop. TRIGGER when: user reports a bug, regression, or unexpected behaviour in Python code with a traceback, failing test, or issue number; phrases: \"fix
Open skill - /plan
Analysis-only planning — classify and scope a task without writing code; outputs a structured plan to .plans/active/. TRIGGER when: user wants to understand scope and risks before implementation; phrases: \"plan this\", \"scope out X\", \"what would it take to Y\", \"analyse
Open skill - /refactor
Test-first refactoring — audit coverage, add characterization tests, apply changes with safety net, run quality stack and review loop. TRIGGER when: user wants to restructure existing Python code without changing behaviour; phrases: \"refactor X\", \"clean up Y\", \"extract Z\",
Open skill - /review
Multi-agent code review of local Python files, directories, or the current git diff covering architecture, tests, performance, docs, lint, security, and API design. Scope: Python source files in local working tree. Python-file-free targets (pure JS/TS/Go/Rust projects) are out
Open skill

