Experiment

From SethGammon/Citadel · View interactive version

Experiment is an AI agent skill from the GitHub repository SethGammon/Citadel. Automated optimization loop with scalar fitness function. Proposes changes in isolated worktrees, measures against a fixed baseline, and retains verified candidates locally. Supports convergence detection and diminishing returns.

How to use this skill

Skill file

/experiment — Metric-Driven Optimization Loop

Identity

Coordinate bounded metric-driven experiments in isolated checkouts, retaining

candidates locally until the declared verification and independent review pass.

Orientation

When to Use

Inputs

The user provides three things:

  1. scope: Files to modify (glob pattern, e.g., "src/api/*/.ts")
  2. metric: Project command that outputs a single number (e.g., node tools/bundle-bytes.cjs)
  3. budget: Iteration cap (default: 5) or time cap (e.g., "10 minutes")

Ask only for missing inputs that cannot be resolved from the request or project.

The metric MUST exit successfully and output a single finite number to stdout.

Protocol

Step 1: BASELINE

  1. Read project instructions and inspect the checkout, index, untracked files,

and active worktrees. Record the source commit and dirty-file hashes/status.

Leave user work in place: never stash, reset, clean, switch branches, or commit

unrelated changes to make an experiment possible.

  1. Create a dedicated worktree at an explicit commit, or a separate local clone

at that commit. Verify its path, HEAD, and clean status before running commands.

If the requested baseline includes authorized working changes, copy only those

changes into the isolated checkout, preserving deletions and file contents;

verify their hashes and commit the snapshot there. Leave the original index and

files untouched. If the intended snapshot is unclear, clarify before copying.

  1. Before iterations, write the evaluation plan in the experiment report: source

and snapshot commits, allowed source scope, metric command and direction,

timeout, repeat/noise policy, and required typecheck, regression and behavioral

checks. Record any inapplicable check and its reason now, not after a failure.

Ask about direction only if ambiguous. A scalar is a proxy, not acceptance.

  1. Freeze the evaluator: record hashes for the metric implementation, test

assertions, fixtures/expected outputs, and relevant command/config files.

These are outside candidate edit scope, even if a broad glob includes them.

Keep the original incumbent commit, evaluator and plan fixed for the run.

  1. Pin one file-byte representation before measuring, including checkout filters

and line endings. Compare baseline and evaluator hashes with their committed

blobs; for commit-based evaluation, materialize matching bytes in the isolated

checkout. Use the same representation for every candidate and independent

check; a clean Git status alone does not establish byte identity.

  1. Measure the baseline and run the declared checks in isolation. Record commands,

exit codes, values and evidence paths. An invalid baseline or required-check

failure holds optimization until resolved; do not silently waive the check.

Run commands from the intended isolated checkout. Bind CLAUDE_PROJECT_DIR for

each child process to that checkout so timeout/telemetry helpers cannot write into

the user's source checkout. Use the installed timeout helper (in Citadel itself,

node scripts/run-with-timeout.js 300 <command> [args...]).

Step 2: ITERATE

For each iteration (up to budget):

  1. Create isolation: Use a fresh worktree/clone from the original incumbent or

the last verified retained candidate; record that parent commit. Give the worker

the fixed plan, scope, evaluator exclusions and previous attempts. Verify actual

isolation rather than relying on a branch name or a spawn option alone.

  1. Propose change: Modify only allowed source files. Never change the metric,

tests, expected outputs, exclusions or thresholds to earn a pass. If the

evaluator needs repair, stop this run and establish a new baseline and plan.

  1. Inspect and measure: Check the full diff and evaluator hashes first. Reject

scope violations or evaluator changes, even if they improve the number. Run the

unchanged metric and declared checks with bounded timeouts and the same setup

as the incumbent. Keep raw results, candidate commit and evaluator identity.

  1. Review: For an improving candidate, obtain independent review of the exact

diff, scope, behavior/regressions and evidence. The proposer cannot approve its

own result. A summary-only HANDOFF check is insufficient. Review unavailability,

stale evidence, timeout or an incomplete required check means HELD, not passed.

  1. Decide:

check and independent review passed for this exact candidate. Retain its local

commit; it may seed the next iteration. Do not merge into the user's checkout.

tampering. Log why; preserve evidence before removing only experiment-owned

disposable worktrees. Never clean another agent's or the user's worktree.

reason; it cannot seed a subsequent iteration or authorize promotion.

  1. Log: Record value, delta against both the original baseline and the previous

retained best, verdict, change, commit, required-check results and review evidence.

Promotion is separate from KEEP. Only after all gates pass may an already

authorized local integration proceed into a safe, clean destination. Recheck the

resulting integration subject with the declared checks and independent review if

its content differs. Otherwise leave the candidate local and report the held gate.

Never push, publish, or overwrite dirty work as part of the optimization loop.

Step 3: CONVERGENCE CHECK

After each iteration, check:

do not count a held candidate as an accepted improvement

Calculate diminishing returns against the previous retained best. Keep reporting

overall improvement against the original baseline; never replace that baseline.

For a zero baseline, report absolute delta instead of dividing by zero.

Step 4: REPORT

Create and update .planning/research/experiment-{unique-slug}.md in the isolated

run checkout; do not overwrite another run's report. Include the frozen plan and

identities from Step 1, followed by:

# Experiment: {Description}

> Metric: `{command}`
> Direction: {lower|higher} is better
> Scope: {glob pattern}
> Budget: {N iterations}
> Date: {ISO date}
> Original incumbent: {commit and evaluator hashes}
> Checks: {commands, acceptance conditions, repeat/noise policy}

## Results

| Iteration / Commit | Value | Delta from original / best | Verdict | Checks / Review / Evidence |
|--------------------|-------|----------------------------|---------|----------------------------|
| baseline / {sha}   | {N}   | —                          | —       | {paths}                    |
| 1 / {sha}          | {N}   | {+/-} / {+/-}              | KEEP    | {paths}                    |
| 2 / {sha}          | {N}   | {+/-} / {+/-}              | HELD    | {reason and paths}         |

## Outcome
- **Start**: {baseline}
- **Best verified local candidate**: {value and commit, or none}
- **Improvement**: {percentage}
- **Iterations**: {kept}/{total}; held: {N}
- **Stop reason**: {convergence|diminishing|budget|held gate}
- **Integration**: {not attempted|held with reason|verified target commit}
- **Source checkout preservation**: {before/after dirty-file hashes and index/status comparison}

## Retained Local Changes
{Changes, commit hashes, checkout paths, and any unresolved gates}

Log the run through the existing integrity-aware telemetry writer, with the

isolated run checkout as CLAUDE_PROJECT_DIR:

node .citadel/scripts/telemetry-log.cjs --event agent-complete --agent experiment --session {slug} --status {success|partial|failed}

In Citadel itself use scripts/telemetry-log.cjs. Put detailed results in the

report; use partial for held gates. The writer supplies _hash and _hash_v: 1.

If unavailable, report that limitation; never append raw JSONL or fabricate hashes.

Common Metrics

| Goal | Numeric output from the project metric command |

|------|-----------------------------------------------|

| Reduce bundle size | Built bundle bytes; a failed build is a failed measurement |

| Increase throughput | Operations per second under the fixed workload |

| Reduce latency | A predeclared percentile in milliseconds |

| Reduce file/line count | Count within a fixed scope, with behavioral checks retained |

Safety Rules

Contextual Gates

Disclosure: "Running isolated experiment on [target] from [commit], with metric [command] and checks [plan]. Budget: [N]. Verified candidates remain local until integration gates pass."

Reversibility: amber — experiment-owned local commits/checkouts are retained with evidence; the source checkout stays intact.

Trust gates:

Trust level never waives isolation, frozen evaluation, review or required checks.

Quality Gates

Fringe Cases

Metric outputs nothing, non-numeric text, NaN/infinity, or exits nonzero:

Invalid measurement. Resolve the baseline command before starting; discard a

candidate measurement failure. Pipelines must preserve the measured command's

failure status rather than hiding it behind a successful text filter.

No worktree support: Use a separate clean local clone at the recorded commit.

A branch in the same checkout is not isolation. If neither is possible, stop as

held and provide the plan; do not edit the source checkout.

If .planning/research/ does not exist: Create it before writing the experiment report. If .planning/ itself doesn't exist, create the full path or output the report inline.

Budget exhausted with zero kept iterations: Report outcome as "no improvement found". This is a valid result — do not continue past the budget.

Exit Protocol

---HANDOFF---
- Experiment: {description}
- Result: {original baseline} → {best verified local value}; retained {N}/{total}
- Gates / integration: {passed, held or failed; target commit if integrated}
- Stop reason / report: {reason}; {isolated report path}
- Preservation / recovery: {source hashes unchanged or observed discrepancy}; {retained local commits and paths}
---
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.