repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

feat(tune): add auto-resume limit config

8e7cf292db6fef0b68dc1f906c66226dd14547cb

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

extensions/tune/README.md

Modified
32 32 - `shortcuts.toggleDashboard` — default `ctrl+shift+t`; `null` disables
33 33 - `shortcuts.fullscreenDashboard` — default `ctrl+shift+f`; `null` disables
35 +Run config file: `.pi/tune/config.json`.
37 +Keys:
39 +- `maxIterations` — positive number caps experiments per segment; default `null`
40 +- `autoResumeLimit` — positive number caps auto-resume turns; default `null`
35 42 ## Hooks
37 44 Pi hooks:
56 63 All state lives in `.pi/tune/`.
58 -| File | Purpose |
59 -| ------------------ | --------------------------------------------- |
60 -| `session.jsonl` | Append-only experiment log |
61 -| `session.md` | Living document: objective, what's been tried |
62 -| `ideas.md` | Backlog of untried optimizations |
63 -| `benchmark.mjs` | Benchmark script |
64 -| `checks.mjs` | Optional correctness gate |
65 -| `config.json` | Optional `maxIterations` config |
66 -| `hooks/before.mjs` | Optional pre-iteration hook |
67 -| `hooks/after.mjs` | Optional post-iteration hook |
65 +| File | Purpose |
66 +| ------------------ | --------------------------------------------------- |
67 +| `session.jsonl` | Append-only experiment log |
68 +| `session.md` | Living document: objective, what's been tried |
69 +| `ideas.md` | Backlog of untried optimizations |
70 +| `benchmark.mjs` | Benchmark script |
71 +| `checks.mjs` | Optional correctness gate |
72 +| `config.json` | Optional `maxIterations` / `autoResumeLimit` config |
73 +| `hooks/before.mjs` | Optional pre-iteration hook |
74 +| `hooks/after.mjs` | Optional post-iteration hook |
69 76 ## Behavior
105 112 Compaction skips the default LLM summary and rebuilds a deterministic summary
106 113 from persisted tune state.
115 +`.pi/tune/config.json` may set `maxIterations` to cap experiments per segment and
116 +`autoResumeLimit` to cap automatic resume turns.
117 +`autoResumeLimit` defaults to `null`, meaning unlimited.

extensions/tune/__tests__/unit.test.ts

Modified
1 +import * as fs from "node:fs";
2 +import * as os from "node:os";
1 3 import * as path from "node:path";
2 4 import { DEFAULT_MAX_LINES } from "@earendil-works/pi-coding-agent";
3 5 import { describe, expect, it } from "vitest";
57 +// ── Tune config ───────────────────────────────────────────────────────────
59 +describe("readAutoResumeLimit", () => {
60 + function withTuneConfig(contents?: string): string {
61 + const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "pi-ext-tune-"));
62 + if (contents !== undefined) {
63 + const dir = path.join(cwd, ".pi", "tune");
64 + fs.mkdirSync(dir, { recursive: true });
65 + fs.writeFileSync(path.join(dir, "config.json"), contents);
66 + }
67 + return cwd;
68 + }
70 + it("defaults to unlimited", () => {
71 + for (const contents of [undefined, "{}", '{"autoResumeLimit":null}']) {
72 + const cwd = withTuneConfig(contents);
73 + try {
74 + expect(__test.readAutoResumeLimit(cwd)).toBeNull();
75 + } finally {
76 + fs.rmSync(cwd, { recursive: true, force: true });
77 + }
78 + }
79 + });
81 + it("uses positive numeric limits", () => {
82 + const cwd = withTuneConfig('{"autoResumeLimit":12.9}');
83 + try {
84 + expect(__test.readAutoResumeLimit(cwd)).toBe(12);
85 + } finally {
86 + fs.rmSync(cwd, { recursive: true, force: true });
87 + }
88 + });
90 + it("treats invalid limits as unlimited", () => {
91 + for (const contents of [
92 + '{"autoResumeLimit":0}',
93 + '{"autoResumeLimit":-1}',
94 + '{"autoResumeLimit":"20"}',
95 + ]) {
96 + const cwd = withTuneConfig(contents);
97 + try {
98 + expect(__test.readAutoResumeLimit(cwd)).toBeNull();
99 + } finally {
100 + fs.rmSync(cwd, { recursive: true, force: true });
103 + });
104 +});
55 106 // ── Number formatting ─────────────────────────────────────────────────────
57 108 describe("formatNum", () => {

extensions/tune/index.ts

Modified
487 +function readAutoResumeLimit(cwd: string): number | null {
488 + try {
489 + const configPath = tuneConfigPath(cwd);
490 + if (!fs.existsSync(configPath)) return null;
491 + const config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
492 + return typeof config.autoResumeLimit === "number" &&
493 + Number.isFinite(config.autoResumeLimit) &&
494 + config.autoResumeLimit > 0
495 + ? Math.floor(config.autoResumeLimit)
496 + : null;
497 + } catch {
498 + return null;
487 502 // ── State factories ───────────────────────────────────────────────────────
489 504 function cloneExperimentState(state: ExperimentState): ExperimentState {
1122 1137 detectVcsKind,
1123 1138 keepVcsChanges,
1124 1139 discardVcsChanges,
1140 + readAutoResumeLimit,
1127 1143 // ── Extension ─────────────────────────────────────────────────────────────
1129 1145 export default function tuneExtension(pi: ExtensionAPI) {
1130 - const MAX_AUTORESUME_TURNS = 20;
1131 1146 const BENCHMARK_GUARDRAIL =
1132 1147 "Be careful not to overfit to the benchmarks and do not cheat on the benchmarks.";
1133 1148 const SETTLED_WINDOW_MS = 800;
1190 1205 return;
1192 1207 if (!isAgentSettled(ctx)) return;
1193 - if (hasReachedAutoResumeLimit(runtime)) {
1208 + const autoResumeLimit = reachedAutoResumeLimit(ctx, runtime);
1209 + if (autoResumeLimit !== null) {
1194 1210 cancelPendingResume(runtime);
1195 - notifyAutoResumeLimitReached(ctx);
1211 + notifyAutoResumeLimitReached(ctx, autoResumeLimit);
1196 1212 return;
1198 1214 cancelPendingResume(runtime);
1231 1247 const shouldAutoResumeAfterCompact = (runtime: TuneRuntime): boolean =>
1232 1248 runtime.tuneMode;
1234 - const hasReachedAutoResumeLimit = (runtime: TuneRuntime): boolean =>
1235 - runtime.autoResumeTurns >= MAX_AUTORESUME_TURNS;
1250 + const reachedAutoResumeLimit = (
1251 + ctx: ExtensionContext,
1252 + runtime: TuneRuntime,
1253 + ): number | null => {
1254 + const limit = readAutoResumeLimit(ctx.cwd);
1255 + return limit !== null && runtime.autoResumeTurns >= limit ? limit : null;
1256 + };
1237 - const notifyAutoResumeLimitReached = (ctx: ExtensionContext): void => {
1238 - ctx.ui.notify(
1239 - `Tune auto-resume limit reached (${MAX_AUTORESUME_TURNS} turns)`,
1240 - "info",
1241 - );
1258 + const notifyAutoResumeLimitReached = (
1259 + ctx: ExtensionContext,
1260 + limit: number,
1261 + ): void => {
1262 + ctx.ui.notify(`Tune auto-resume limit reached (${limit} turns)`, "info");
1244 1265 const composeResumeMessage = (_ctx: ExtensionContext): string => {
1699 1720 return;
1701 1722 if (!gate(runtime)) return;
1702 - if (hasReachedAutoResumeLimit(runtime)) {
1703 - notifyAutoResumeLimitReached(ctx);
1723 + const autoResumeLimit = reachedAutoResumeLimit(ctx, runtime);
1724 + if (autoResumeLimit !== null) {
1725 + notifyAutoResumeLimitReached(ctx, autoResumeLimit);
1704 1726 return;
1706 1728 schedulePendingResume(ctx, runtime, composeMessage(ctx));