Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(coding-agent): derive refine output budgets from the model (#598)

Seth Karten2c651cee7e7f
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index fae19b0624..884f59eca5 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -2,6 +2,10 @@
## [Unreleased]
+### Fixed
+
+- Fixed `/refine` failing with an opaque JSON parse error when the refiner exceeded a fixed 4096-token output cap; output budgets now derive from the selected model, and a truncated reply reports the exhausted budget directly.
+
## [0.5.0] - 2026-08-03
### Breaking Changes
diff --git a/packages/coding-agent/src/core/refinement/refinement.ts b/packages/coding-agent/src/core/refinement/refinement.ts
index 589d5259c8..c7ce97ee6e 100644
--- a/packages/coding-agent/src/core/refinement/refinement.ts
+++ b/packages/coding-agent/src/core/refinement/refinement.ts
@@ -184,6 +184,26 @@ Return JSON only:
"instructions": "optional concise instructions for /refine if shouldRefine is true"
}`;
+/**
+ * Output budgets are derived from the selected model instead of fixed literals.
+ * /refine input scales with harness size (entry overview, refinement history, and
+ * the trajectory slice), so a constant output cap silently truncates exactly the
+ * large multi-edit proposals that matter most. Math.min keeps small models honest.
+ */
+const REFINEMENT_MAX_OUTPUT_TOKENS = 32_000;
+const AUTO_REFINE_REVIEW_MAX_OUTPUT_TOKENS = 4_096;
+
+const TRUNCATED_JSON_ERROR =
+ "the model stopped before completing its JSON object. This usually means the output budget was exhausted; retry with a smaller request.";
+
+function refinementMaxOutputTokens(model: Model<any>): number {
+ return Math.min(model.maxTokens, REFINEMENT_MAX_OUTPUT_TOKENS);
+}
+
+function autoRefineReviewMaxOutputTokens(model: Model<any>): number {
+ return Math.min(model.maxTokens, AUTO_REFINE_REVIEW_MAX_OUTPUT_TOKENS);
+}
+
function now(): string {
return new Date().toISOString();
}
@@ -541,19 +561,71 @@ function historyForPrompt(history: RefinementResult[]): string {
.join("\n\n");
}
+/**
+ * Whether a JSON candidate ends mid-value: an unterminated string, or unclosed
+ * objects/arrays. A reply cut off by an exhausted output budget is incomplete in
+ * this sense, while a complete-but-malformed reply is balanced. Brace slicing can
+ * also produce a balanced fragment, so callers treat "balanced" as malformed.
+ */
+function isIncompleteJson(candidate: string): boolean {
+ let depth = 0;
+ let inString = false;
+ let escaped = false;
+ for (const char of candidate) {
+ if (escaped) {
+ escaped = false;
+ continue;
+ }
+ if (inString) {
+ if (char === "\\") escaped = true;
+ else if (char === '"') inString = false;
+ continue;
+ }
+ if (char === '"') inString = true;
+ else if (char === "{" || char === "[") depth++;
+ else if (char === "}" || char === "]") depth--;
+ }
+ return inString || depth > 0;
+}
+
+function parseJsonCandidate(candidate: string): unknown {
+ try {
+ return JSON.parse(candidate);
+ } catch (error) {
+ // A truncated reply and a malformed one both fail here, and JSON.parse
+ // describes the fragment rather than the cause. Name the cause instead.
+ if (isIncompleteJson(candidate)) {
+ throw new Error(TRUNCATED_JSON_ERROR);
+ }
+ throw new Error(`the model did not return valid JSON: ${error instanceof Error ? error.message : String(error)}`);
+ }
+}
+
function extractJsonObject(text: string): unknown {
const trimmed = text.trim();
if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
- return JSON.parse(trimmed);
+ // A reply truncated after a nested closing brace still looks well-formed
+ // here, so this path needs the same diagnosis as the slicing fallback.
+ return parseJsonCandidate(trimmed);
}
const fenced = trimmed.match(/```(?:json)?\s*([\s\S]*?)```/);
if (fenced) {
- return JSON.parse(fenced[1].trim());
+ return parseJsonCandidate(fenced[1].trim());
}
+ // Brace slicing recovers JSON wrapped in prose. On a reply truncated inside the
+ // edits array it slices to an earlier edit's closing brace, so a failure here
+ // is diagnosed against the original text rather than the balanced fragment.
const start = trimmed.indexOf("{");
const end = trimmed.lastIndexOf("}");
if (start !== -1 && end > start) {
- return JSON.parse(trimmed.slice(start, end + 1));
+ try {
+ return JSON.parse(trimmed.slice(start, end + 1));
+ } catch {
+ return parseJsonCandidate(trimmed.slice(start));
+ }
+ }
+ if (isIncompleteJson(trimmed)) {
+ throw new Error(TRUNCATED_JSON_ERROR);
}
throw new Error("Refiner did not return a JSON object");
}
@@ -844,12 +916,15 @@ export async function planRefinement(
systemPrompt: REFINEMENT_SYSTEM_PROMPT,
messages: [{ role: "user", content: [{ type: "text", text: userPrompt }], timestamp: Date.now() }],
},
- { maxTokens: 4096, signal, apiKey, headers },
+ { maxTokens: refinementMaxOutputTokens(model), signal, apiKey, headers },
);
if (response.stopReason === "error") {
throw new Error(`Refinement failed: ${response.errorMessage || "Unknown error"}`);
}
+ if (response.stopReason === "length") {
+ throw new Error(`Refinement failed: ${TRUNCATED_JSON_ERROR}`);
+ }
const text = response.content
.filter((content): content is { type: "text"; text: string } => content.type === "text")
@@ -907,11 +982,14 @@ ${conversationText}
systemPrompt: AUTO_REFINE_REVIEW_SYSTEM_PROMPT,
messages: [{ role: "user", content: [{ type: "text", text: userPrompt }], timestamp: Date.now() }],
},
- { maxTokens: 1024, signal, apiKey, headers },
+ { maxTokens: autoRefineReviewMaxOutputTokens(model), signal, apiKey, headers },
);
if (response.stopReason === "error") {
throw new Error(`Auto-refine review failed: ${response.errorMessage || "Unknown error"}`);
}
+ if (response.stopReason === "length") {
+ throw new Error(`Auto-refine review failed: ${TRUNCATED_JSON_ERROR}`);
+ }
const text = response.content
.filter((content): content is { type: "text"; text: string } => content.type === "text")
.map((content) => content.text)
diff --git a/packages/coding-agent/test/refinement.test.ts b/packages/coding-agent/test/refinement.test.ts
index 79966a475c..3b2f7037cf 100644
--- a/packages/coding-agent/test/refinement.test.ts
+++ b/packages/coding-agent/test/refinement.test.ts
@@ -1030,8 +1030,9 @@ describe("harness refinement", () => {
"During a local refinement, global entries are read-only context: never propose update or delete edits for them",
),
});
+ // Budget is derived from the model (8192) rather than a fixed literal.
expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({
- maxTokens: 4096,
+ maxTokens: 8192,
apiKey: "api-key",
headers: { "x-test-header": "1" },
});
@@ -1047,6 +1048,77 @@ describe("harness refinement", () => {
);
});
+ it("caps the refinement output budget by the model's own maxTokens", async () => {
+ const state = loadHarnessState(makeTempDir());
+ completeSimpleMock.mockResolvedValueOnce(
+ assistantText(JSON.stringify({ summary: "s", rationale: "r", expectedOutcome: "o", edits: [] })),
+ );
+ // A large model must receive the policy ceiling, not its full output width.
+ const wideModel = { ...createRefineModel(false), maxTokens: 128_000 };
+
+ await refineHarness([], state, [], wideModel, "api-key", {});
+
+ expect(completeSimpleMock.mock.calls[0][2]).toMatchObject({ maxTokens: 32_000 });
+ });
+
+ it("reports an exhausted output budget instead of a JSON parse error", async () => {
+ const state = loadHarnessState(makeTempDir());
+ // A reply truncated inside the edits array: brace slicing would otherwise
+ // recover a fragment and surface an opaque JSON.parse message.
+ const truncated = `{
+ "summary": "s",
+ "rationale": "r",
+ "expectedOutcome": "o",
+ "edits": [
+ { "action": "create", "kind": "memory", "id": "a", "title": "t", "content": "first" },
+ { "action": "create", "kind": "memory", "id": "b", "title": "t2", "content": "second`;
+ completeSimpleMock.mockResolvedValueOnce({ ...assistantText(truncated), stopReason: "length" });
+
+ await expect(refineHarness([], state, [], createRefineModel(false), "api-key", {})).rejects.toThrow(
+ /output budget was exhausted/,
+ );
+ });
+
+ it("rejects a truncated proposal that never reports a length stop reason", async () => {
+ const state = loadHarnessState(makeTempDir());
+ const truncated = `{
+ "summary": "s",
+ "edits": [
+ { "action": "create", "kind": "memory", "id": "a", "title": "t", "content": "first" },
+ { "action": "create", "kind": "memory", "id": "b", "title": "t2", "content": "second`;
+ completeSimpleMock.mockResolvedValueOnce(assistantText(truncated));
+
+ await expect(refineHarness([], state, [], createRefineModel(false), "api-key", {})).rejects.toThrow(
+ /stopped before completing its JSON object/,
+ );
+ });
+
+ it("reports truncation when a JSON-only reply is cut after a nested closing brace", async () => {
+ const state = loadHarnessState(makeTempDir());
+ // Ends on "}" so it takes the startsWith/endsWith fast path rather than
+ // the brace-slicing fallback, but is still an incomplete object.
+ const truncated = `{
+ "summary": "s",
+ "edits": [
+ { "action": "create", "kind": "memory", "id": "a", "title": "t", "content": "first" }`;
+ completeSimpleMock.mockResolvedValueOnce(assistantText(truncated));
+
+ await expect(refineHarness([], state, [], createRefineModel(false), "api-key", {})).rejects.toThrow(
+ /stopped before completing its JSON object/,
+ );
+ });
+
+ it("reports malformed JSON as invalid rather than as an exhausted budget", async () => {
+ const state = loadHarnessState(makeTempDir());
+ // Complete and balanced, but not valid JSON: this is a model formatting
+ // failure, not a truncation, and must not blame the output budget.
+ completeSimpleMock.mockResolvedValueOnce(assistantText('Here is the result: {"edits": [oops]}'));
+
+ await expect(refineHarness([], state, [], createRefineModel(false), "api-key", {})).rejects.toThrow(
+ /did not return valid JSON/,
+ );
+ });
+
it("rolls back created, updated, and deleted entries from refinement history", async () => {
const state = loadHarnessState(makeTempDir());
seedEntry(state, "memory", "kept_memory");