repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

Add one-time legacy .pi/autoresearch to .pi/tune state migration and tests

744beee2767c6f0b57ba192bb0896881373083b1

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Result: {"status":"keep","wall_time":500,"tests_passed":45,"failures":0}

Changed files (3)

extensions/tune/README.md

Modified
31 31 ## State
33 -All state lives in `.pi/tune/`:
33 +All state lives in `.pi/tune/`.
34 +If `.pi/tune/` is missing and legacy `.pi/autoresearch/` exists, tune copies the legacy state once on session load/init.
35 36 | File | Purpose |
36 37 |---|---|
46 47 ## Behavior
48 -Tunes through an autonomous loop: edit → commit → benchmark → log → keep or revert → repeat.
49 +Tunes through an autonomous loop: edit → benchmark → log → commit or revert → repeat.
50 51 Confidence scoring (MAD-based) after 3+ runs: ≥2.0× = real, 1.0–2.0× = marginal, <1.0× = noise.

extensions/tune/__tests__/remaining.test.ts

Modified
1 -import { beforeAll, describe, expect, it } from "bun:test";
1 +import { afterEach, beforeAll, describe, expect, it } from "bun:test";
2 2 import * as fs from "node:fs";
3 3 import * as path from "node:path";
4 4 import {
11 11 import { __test } from "../index.ts";
13 13 const TESTDIR = "/tmp/pi-ext-20260530-164209-project";
14 +const MIGRATION_TESTDIR = "/tmp/pi-ext-tune-migration";
14 15 const TEST_JSONL = [
15 16 '{"type":"config","name":"Optimize sqrt/sin loop","metricName":"total_ms","metricUnit":"ms","bestDirection":"lower"}',
16 17 '{"run":1,"commit":"92d4b83","metric":7,"metrics":{},"status":"keep","description":"Baseline: unoptimized sqrt/sin loop","timestamp":1780152220658,"segment":0,"confidence":null,"asi":{"hypothesis":"Baseline measurement"}}',
26 27 fs.writeFileSync(path.join(tuneDir, "session.jsonl"), TEST_JSONL);
30 +afterEach(() => {
31 + fs.rmSync(MIGRATION_TESTDIR, { recursive: true, force: true });
32 +});
29 34 // ── 1. COMPACTION RESUME ────────────────────────────────────────────────
31 36 describe("compaction resume", () => {
102 -// ── 3. SHORTCUTS ──────────────────────────────────────────────────────
107 +// ── 3. STATE MIGRATION ─────────────────────────────────────────────────
109 +describe("state migration", () => {
110 + it("copies legacy .pi/autoresearch state into .pi/tune once", () => {
111 + const legacyDir = path.join(MIGRATION_TESTDIR, ".pi", "autoresearch");
112 + const tuneDir = path.join(MIGRATION_TESTDIR, ".pi", "tune");
113 + fs.mkdirSync(legacyDir, { recursive: true });
114 + fs.writeFileSync(path.join(legacyDir, "session.jsonl"), TEST_JSONL);
115 + fs.writeFileSync(path.join(legacyDir, "ideas.md"), "- keep this\n");
117 + expect(__test.migrateLegacyTuneState(MIGRATION_TESTDIR)).toBe(true);
118 + expect(fs.readFileSync(path.join(tuneDir, "session.jsonl"), "utf-8")).toBe(
119 + TEST_JSONL,
120 + );
121 + expect(fs.readFileSync(path.join(tuneDir, "ideas.md"), "utf-8")).toBe(
122 + "- keep this\n",
123 + );
124 + expect(fs.existsSync(path.join(legacyDir, "session.jsonl"))).toBe(true);
125 + expect(__test.migrateLegacyTuneState(MIGRATION_TESTDIR)).toBe(false);
126 + });
127 +});
129 +// ── 4. SHORTCUTS ──────────────────────────────────────────────────────
104 131 describe("shortcuts", () => {
105 132 it("returns defaults when no config file", () => {
112 -// ── 4. HOOK PATHS ─────────────────────────────────────────────────────
139 +// ── 5. HOOK PATHS ─────────────────────────────────────────────────────
114 141 describe("hook paths", () => {
115 142 it("resolves to .pi/tune/hooks/{stage}.mjs", () => {
125 -// ── 5. EXPORT DASHBOARD ───────────────────────────────────────────────
152 +// ── 6. EXPORT DASHBOARD ───────────────────────────────────────────────
127 154 describe("export dashboard", () => {
128 155 it("ships a dashboard template next to the tune extension", () => {

extensions/tune/index.ts

Modified
58 58 const EXPERIMENT_MAX_BYTES = 4 * 1024;
60 60 const STATE_DIR = ".pi/tune";
61 +const LEGACY_STATE_DIR = ".pi/autoresearch";
62 63 const TUNE_SUBCOMMAND_COMPLETIONS: AutocompleteItem[] = [
63 64 { value: "off", label: "off", description: "Turn tune mode off" },
342 343 // ── State path helpers ────────────────────────────────────────────────────
344 345 const stateDir = (dir: string) => path.join(dir, STATE_DIR);
346 +const legacyStateDir = (dir: string) => path.join(dir, LEGACY_STATE_DIR);
345 347 const tuneJsonlPath = (dir: string) => path.join(stateDir(dir), "session.jsonl");
346 348 const tuneMdPath = (dir: string) => path.join(stateDir(dir), "session.md");
347 349 const tuneIdeasPath = (dir: string) => path.join(stateDir(dir), "ideas.md");
349 351 const tuneScriptPath = (dir: string) => path.join(stateDir(dir), "benchmark.mjs");
350 352 const tuneConfigPath = (dir: string) => path.join(stateDir(dir), "config.json");
354 +function migrateLegacyTuneState(cwd: string): boolean {
355 + const current = stateDir(cwd);
356 + const legacy = legacyStateDir(cwd);
357 + try {
358 + if (fs.existsSync(current) || !fs.existsSync(legacy)) return false;
359 + fs.cpSync(legacy, current, { recursive: true });
360 + return true;
361 + } catch {
362 + return false;
352 366 function findBaselineMetric(results: ExperimentResult[], segment: number): number | null {
353 367 const cur = currentResults(results, segment);
354 368 return cur.length > 0 ? cur[0].metric : null;
867 881 dashboardTemplatePath,
868 882 tuneArgumentCompletions,
869 883 tuneCommandArgumentSuggestions,
884 + migrateLegacyTuneState,
872 887 // ── Extension ─────────────────────────────────────────────────────────────
1107 1122 runtime.autoResumeTurns = 0;
1108 1123 runtime.state = createExperimentState();
1125 + migrateLegacyTuneState(ctx.cwd);
1110 1126 const state = runtime.state;
1111 1127 const jsonlPath = tuneJsonlPath(ctx.cwd);
1112 1128 let loadedFromJsonl = false;
1477 1493 state.bestMetric = null;
1478 1494 state.secondaryMetrics = [];
1479 1495 state.confidence = null;
1496 + migrateLegacyTuneState(ctx.cwd);
1480 1497 state.maxExperiments = readMaxExperiments(ctx.cwd);
1482 1499 // Ensure state directory exists