Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions extensions/repl-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ export {
clampModelLimits,
clampRlmLimits,
defaultRlmBudget,
defaultRlmMaxIterations,
formatRlmResult,
} from "../src/model_boundary.js";
import {
buildRlmRegistry,
clampModelLimits,
clampRlmLimits,
defaultRlmBudget,
defaultRlmMaxIterations,
formatRlmResult,
} from "../src/model_boundary.js";

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1699,6 +1702,7 @@ export default function (pi: ReplExtensionApi) {
void runRlm(question, {
llmClient,
registry,
maxIterations: defaultRlmMaxIterations(),
budget: defaultRlmBudget(),
signal: controller.signal,
}).then((result) => {
Expand Down
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/mcp_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
clampModelLimits,
clampRlmLimits,
defaultRlmBudget,
defaultRlmMaxIterations,
formatRlmResult,
} from "./model_boundary.js";
import { closeSandboxPool } from "./pool.js";
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down
27 changes: 21 additions & 6 deletions src/model_boundary.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;

Expand All @@ -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.
*/
Expand All @@ -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);
Expand Down
11 changes: 9 additions & 2 deletions src/rlm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ────────────────────────────────────────────────────

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1261,7 +1268,7 @@ export async function runRlm(question: string, options: RlmOptions): Promise<Rlm
}

const llmClient = options.llmClient;
const maxIterations = options.maxIterations ?? 10;
const maxIterations = options.maxIterations ?? DEFAULT_RLM_MAX_ITERATIONS;
const iterations: RlmIteration[] = [];
// Assigned after the merged registry exists (D50): the default system
// prompt is built from the live registry, so it cannot be a static const.
Expand Down
29 changes: 28 additions & 1 deletion test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
clampModelLimits,
clampRlmLimits,
defaultRlmBudget,
defaultRlmMaxIterations,
formatRlmResult,
} from "../extensions/repl-extension.js";
// The #35 constants are read off the namespace rather than named-imported so
Expand Down Expand Up @@ -984,21 +985,26 @@ describe("repl extension — clampModelLimits", () => {

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,
});
});
Expand All @@ -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 ────────────
Expand Down
11 changes: 6 additions & 5 deletions test/mcp_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
10 changes: 5 additions & 5 deletions test/rlm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
});

Expand Down
Loading