diff --git a/extensions/repl-extension.ts b/extensions/repl-extension.ts index 678fa2b..555d427 100644 --- a/extensions/repl-extension.ts +++ b/extensions/repl-extension.ts @@ -21,6 +21,7 @@ export { clampModelLimits, clampRlmLimits, defaultRlmBudget, + defaultRlmMaxIterations, formatRlmResult, } from "../src/model_boundary.js"; import { @@ -28,6 +29,7 @@ import { clampModelLimits, clampRlmLimits, defaultRlmBudget, + defaultRlmMaxIterations, formatRlmResult, } from "../src/model_boundary.js"; @@ -1612,7 +1614,8 @@ export default function (pi: ReplExtensionApi) { question: Type.String({ description: "The question to investigate." }), maxIterations: Type.Optional( Type.Number({ - description: "Maximum code-gen iterations before the loop gives up. Default: 10.", + description: + "Maximum code-gen iterations before the loop gives up. Default and cap: 20 (REPL_RLM_MAX_ITERATIONS).", }), ), maxDepth: Type.Optional( @@ -1645,7 +1648,7 @@ export default function (pi: ReplExtensionApi) { const result = await runRlm(params.question, { llmClient, registry, - maxIterations: limits.maxIterations, + maxIterations: limits.maxIterations ?? defaultRlmMaxIterations(), maxDepth: limits.maxDepth, budget, signal, @@ -1699,6 +1702,7 @@ export default function (pi: ReplExtensionApi) { void runRlm(question, { llmClient, registry, + maxIterations: defaultRlmMaxIterations(), budget: defaultRlmBudget(), signal: controller.signal, }).then((result) => { diff --git a/package-lock.json b/package-lock.json index acc2c92..ad6c2a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,9 @@ "undici": "8.9.0", "zod": "4.6.5" }, + "bin": { + "repl-simple-mcp": "dist/mcp_main.js" + }, "devDependencies": { "@biomejs/biome": "2.5.8", "@earendil-works/pi-coding-agent": "^0.84.1", diff --git a/src/mcp_server.ts b/src/mcp_server.ts index 18ca80d..dc00528 100644 --- a/src/mcp_server.ts +++ b/src/mcp_server.ts @@ -10,6 +10,7 @@ import { clampModelLimits, clampRlmLimits, defaultRlmBudget, + defaultRlmMaxIterations, formatRlmResult, } from "./model_boundary.js"; import { closeSandboxPool } from "./pool.js"; @@ -448,7 +449,9 @@ export function createReplMcpServer(options: ReplMcpServerOptions): ReplMcpServe maxIterations: z .number() .optional() - .describe("Maximum code-gen iterations before the loop gives up. Default and cap: 10."), + .describe( + "Maximum code-gen iterations before the loop gives up. Default and cap: 20 (REPL_RLM_MAX_ITERATIONS).", + ), maxDepth: z .number() .optional() @@ -471,7 +474,7 @@ export function createReplMcpServer(options: ReplMcpServerOptions): ReplMcpServe const result = await runRlm(args.question, { llmClient, registry: buildRlmRegistry(options.root), - maxIterations: limits.maxIterations, + maxIterations: limits.maxIterations ?? defaultRlmMaxIterations(), maxDepth: limits.maxDepth, budget: limits.budget ?? defaultRlmBudget(), signal: extra.signal, diff --git a/src/model_boundary.ts b/src/model_boundary.ts index ce1f2ad..f68b0cb 100644 --- a/src/model_boundary.ts +++ b/src/model_boundary.ts @@ -1,6 +1,6 @@ import { limitsConfig } from "./sandbox.js"; import type { RunLimits } from "./types.js"; -import type { RlmResult } from "./rlm.js"; +import { DEFAULT_RLM_MAX_ITERATIONS, type RlmResult } from "./rlm.js"; import { ToolRegistry } from "./registry.js"; import { createPiBridgeTools } from "./bridge.js"; import { createBuiltinTools } from "./builtins.js"; @@ -42,8 +42,22 @@ export function defaultRlmBudget(): number { return Number.isFinite(parsed) ? parsed : DEFAULT_RLM_BUDGET; } -/** Ceiling on a model-supplied `maxIterations` — the loop's default. */ -const RLM_MAX_ITERATIONS = 10; +const RLM_MAX_ITERATIONS_VAR = "REPL_RLM_MAX_ITERATIONS"; + +/** + * The default `rlm` iteration count for this process — and, like the budget, + * also the ceiling on a model-supplied `maxIterations`. The env override is a + * ceiling, not merely a default the model can out-ask: a value below the + * library default still clamps, and a non-integer or non-positive value falls + * back to `DEFAULT_RLM_MAX_ITERATIONS`. + */ +export function defaultRlmMaxIterations(): number { + const raw = process.env[RLM_MAX_ITERATIONS_VAR]; + if (raw === undefined || raw.trim() === "") return DEFAULT_RLM_MAX_ITERATIONS; + const parsed = Number(raw); + return Number.isInteger(parsed) && parsed >= 1 ? parsed : DEFAULT_RLM_MAX_ITERATIONS; +} + /** Ceiling on a model-supplied `maxDepth` — the loop's default. */ const RLM_MAX_DEPTH = 1; @@ -53,8 +67,9 @@ const RLM_MAX_DEPTH = 1; * The `rlm` tool is the same model boundary the `repl` tool clamps through * `clampModelLimits`, so these knobs are clamped, never trusted: * `budget` is capped at `defaultRlmBudget()` (the env override is a ceiling, - * not merely a default the model can out-ask), `maxIterations` at 10 and - * `maxDepth` at 1. A value that is not the right kind of number is omitted + * not merely a default the model can out-ask), `maxIterations` at + * `defaultRlmMaxIterations()` and `maxDepth` at 1. A value that is not the + * right kind of number is omitted * so the loop's own default applies — the model saying nothing and the model * saying nonsense mean the same thing. */ @@ -68,7 +83,7 @@ export function clampRlmLimits( out.budget = Math.min(budget, defaultRlmBudget()); } if (typeof maxIterations === "number" && Number.isInteger(maxIterations) && maxIterations >= 1) { - out.maxIterations = Math.min(maxIterations, RLM_MAX_ITERATIONS); + out.maxIterations = Math.min(maxIterations, defaultRlmMaxIterations()); } if (typeof maxDepth === "number" && Number.isInteger(maxDepth) && maxDepth >= 0) { out.maxDepth = Math.min(maxDepth, RLM_MAX_DEPTH); diff --git a/src/rlm.ts b/src/rlm.ts index 8ced3d1..5bee0a6 100644 --- a/src/rlm.ts +++ b/src/rlm.ts @@ -16,6 +16,13 @@ import { VALUE_RECOVERY, } from "./truncate.js"; +// The default number of code-gen → execute iterations for a `rlm` run when the +// caller omits `maxIterations`. The host model boundary +// (`src/model_boundary.ts`) uses this same value as its ceiling and lets the +// operator override it via `REPL_RLM_MAX_ITERATIONS`; keeping the value here +// gives the library and the boundary one source of truth. +export const DEFAULT_RLM_MAX_ITERATIONS = 20; + // ── RLM types ──────────────────────────────────────────────────── /** @@ -78,7 +85,7 @@ export interface RlmOptions { * callers need NOT restate it (D67). */ systemPrompt?: string; - /** Max RLM iterations before giving up. Default: 10. */ + /** Max RLM iterations before giving up. Default: 20. */ maxIterations?: number; /** Nesting depth limit for `rlm_query` recursion. Default: 1. */ maxDepth?: number; @@ -1261,7 +1268,7 @@ export async function runRlm(question: string, options: RlmOptions): Promise { describe("repl extension — clampRlmLimits", () => { let priorBudget: string | undefined; + let priorMaxIterations: string | undefined; before(() => { priorBudget = process.env.REPL_RLM_BUDGET; + priorMaxIterations = process.env.REPL_RLM_MAX_ITERATIONS; delete process.env.REPL_RLM_BUDGET; + delete process.env.REPL_RLM_MAX_ITERATIONS; }); after(() => { if (priorBudget === undefined) delete process.env.REPL_RLM_BUDGET; else process.env.REPL_RLM_BUDGET = priorBudget; + if (priorMaxIterations === undefined) delete process.env.REPL_RLM_MAX_ITERATIONS; + else process.env.REPL_RLM_MAX_ITERATIONS = priorMaxIterations; }); it("clamps above-cap values to the ceilings", () => { assert.deepEqual(clampRlmLimits(999_999_999, 999, 999), { budget: defaultRlmBudget(), - maxIterations: 10, + maxIterations: 20, maxDepth: 1, }); }); @@ -1022,6 +1028,27 @@ describe("repl extension — clampRlmLimits", () => { process.env.REPL_RLM_BUDGET = "1000"; assert.deepEqual(clampRlmLimits(5000, undefined, undefined), { budget: 1000 }); }); + + it("defaultRlmMaxIterations defaults to 20 and honours REPL_RLM_MAX_ITERATIONS", () => { + assert.equal(defaultRlmMaxIterations(), 20); + process.env.REPL_RLM_MAX_ITERATIONS = "7"; + try { + assert.equal(defaultRlmMaxIterations(), 7); + } finally { + delete process.env.REPL_RLM_MAX_ITERATIONS; + } + process.env.REPL_RLM_MAX_ITERATIONS = "0"; + try { + assert.equal(defaultRlmMaxIterations(), 20); + } finally { + delete process.env.REPL_RLM_MAX_ITERATIONS; + } + }); + + it("caps maxIterations at the operator's REPL_RLM_MAX_ITERATIONS ceiling", () => { + process.env.REPL_RLM_MAX_ITERATIONS = "30"; + assert.deepEqual(clampRlmLimits(undefined, 999, undefined), { maxIterations: 30 }); + }); }); // ── The repl tool wires the clamp into ReplRunner.run ──────────── diff --git a/test/mcp_server.test.ts b/test/mcp_server.test.ts index e7266f9..3f7f480 100644 --- a/test/mcp_server.test.ts +++ b/test/mcp_server.test.ts @@ -595,15 +595,16 @@ describe("MCP server — rlm", () => { }); it("clamps the model-supplied knobs: iterations stop at the ceiling, not at the request", async () => { - // Fourteen non-submitting replies; the ceiling is 10, so the loop must end - // at max_iterations having asked ten times for code plus once for the - // synthesised answer — eleven, never the fourteen it could have consumed. - const llm = cannedLlm(Array.from({ length: 14 }, () => "```python\n1\n```")); + // Twenty-four non-submitting replies; the ceiling is 20, so the loop must + // end at max_iterations having asked twenty times for code plus once for + // the synthesised answer — twenty-one, never the twenty-four it could have + // consumed. + const llm = cannedLlm(Array.from({ length: 24 }, () => "```python\n1\n```")); const h = await connect({ llmClient: llm }); try { const result = await call(h, "rlm", { question: "q", maxIterations: 50 }); assert.match(result.text, /status: max_iterations/); - assert.ok(llm.queries.length <= 11, `asked ${llm.queries.length} times`); + assert.ok(llm.queries.length <= 21, `asked ${llm.queries.length} times`); } finally { await h.close(); } diff --git a/test/rlm.test.ts b/test/rlm.test.ts index 6e080c4..0ace56f 100644 --- a/test/rlm.test.ts +++ b/test/rlm.test.ts @@ -5800,19 +5800,19 @@ describe("runRlm() — defaults (D58)", () => { return new ToolRegistry([]); } - it("defaults maxIterations to 10 (kills M1)", async () => { - // More than 10 code replies, none of which SUBMITs: the loop must stop at + it("defaults maxIterations to 20 (kills M1)", async () => { + // More than 20 code replies, none of which SUBMITs: the loop must stop at // the default cap, not run forever or stop at any other count. const code = "```python\nprint('still working...')\n```"; - const { llm } = mockLlmCodeGen(Array.from({ length: 15 }, () => code)); + const { llm } = mockLlmCodeGen(Array.from({ length: 25 }, () => code)); const result = await runRlm("q", { llmClient: llm, registry: rlmRegistry() }); assert.equal(result.status, "max_iterations"); assert.equal( result.iterations.length, - 10, - "omitting maxIterations must run exactly 10 iterations", + 20, + "omitting maxIterations must run exactly 20 iterations", ); });