Skip to content
Open
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
58 changes: 50 additions & 8 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import { takeTurn } from "./localgate.js";
import { parseLenient } from "./lenient-json.js";
import { LONG_RATE_LIMIT_MS, longQuotaText, rateLimitResetAt, untilText, providerErrorText, normalizeMessage, readStream, transientProviderError, type ProviderError, type StreamAccumulator, type Usage } from "./stream.js";
import { Fragments, SafeStream } from "./live.js";
import { Transcript, toolDetail } from "./transcript.js";
import { Transcript, excerpt, toolDetail } from "./transcript.js";
import { acpAgentFor, acpHealth, acpModels, AcpSession, backendStallMs, isAcp } from "./acp.js";
import { type BackendSession, type ToolRunner } from "./backend.js";
import { endpointProblem, removedSubscriptionProblem } from "./endpoint.js";
Expand Down Expand Up @@ -286,6 +286,10 @@ export function systemPromptFor(cwd: string, extra?: string): string {
/** Fractions of the ceiling at which molt says something, once each. */
const CEILING_WARNINGS = [0.5, 0.8];

/** Malformed tool calls in a row before the model is told firmly, and before the turn ends. */
export const MALFORMED_WARN = 3;
export const MALFORMED_STOP = 6;

/**
* When working history gets compacted, unless told otherwise.
*
Expand Down Expand Up @@ -2350,7 +2354,14 @@ export class Engine {
* all because the clock or a failed request stopped the turn before any check
* ran; two of them had passing work on disk.
*/
private turnEndedBy: "deadline" | "provider" | undefined;
private turnEndedBy: "deadline" | "provider" | "malformed" | undefined;
/**
* Malformed tool calls in a row this turn. One is a slip; a run that kept
* sending broken `act` calls made 17 in a row and paid for every one
* (2026-10-07). After MALFORMED_WARN the model is told firmly; after
* MALFORMED_STOP the turn ends and the work on disk is judged.
*/
private malformedStreak = 0;
/** A subprocess backend went silent past the stall allowance this turn (a provider issue). */
private turnProviderStall = false;
/** `--revert` put this turn's work back, so a person cannot accept it as it stands. */
Expand Down Expand Up @@ -4066,7 +4077,7 @@ export class Engine {
result: BarResult,
attempts: number,
log?: Journal,
endedBy?: "deadline" | "provider",
endedBy?: "deadline" | "provider" | "malformed",
): AsyncGenerator<EngineEvent> {
const onlyWrites = failedOnlyWriteChecks(result);
if (this.cfg.receipts) {
Expand Down Expand Up @@ -4362,6 +4373,7 @@ export class Engine {
this.turnAllRetired = false;
this.turnRevealed = [];
this.turnEndedBy = undefined;
this.malformedStreak = 0;
this.turnProviderStall = false;
this.turnRestored = false;
this.refusedThisTurn = false;
Expand Down Expand Up @@ -4454,7 +4466,7 @@ export class Engine {
outcome = "unverified";
yield {
kind: "info",
text: `the checks passed on the work as it stood when the ${this.turnEndedBy === "deadline" ? "clock" : "provider"} stopped the model, but it never said it was done — unverified.`,
text: `the checks passed on the work as it stood when the ${this.turnEndedBy === "deadline" ? "clock" : this.turnEndedBy === "malformed" ? "malformed-call limit" : "provider"} stopped the model, but it never said it was done — unverified.`,
};
}

Expand Down Expand Up @@ -5055,6 +5067,11 @@ export class Engine {
// sent. Say what actually happened instead.
malformed = true;
}
// An act reaches a tool call as itself only when its actions could not be
// read (expandAct), so it counts as malformed here too.
const known = TOOLS.some((t) => t.function.name === name);
const refused = malformed || name === "act" || !known;
this.malformedStreak = refused ? this.malformedStreak + 1 : 0;
const detail = toolDetail(name, args);
this.turnCalls.add(callId);
// What the current autonomy level says about this exact call. The
Expand Down Expand Up @@ -5128,8 +5145,13 @@ export class Engine {
? `[molt: the arguments for ${name} do not fit its schema, so nothing ran: ` +
`${violations.join("; ")}. Send the call again with them fixed.]`
: `[molt: the arguments for ${name} were not valid JSON, so nothing ran. ` +
`Send them again as a JSON object. What arrived was: ${raw}]`;
this.transcript.push({ role: "tool", tool_call_id: callId, content: complaint });
`Send them again as a JSON object. What arrived began: ${excerpt(raw)}]`;
const firm =
this.malformedStreak >= MALFORMED_WARN
? ` [molt: that is ${this.malformedStreak} malformed calls in a row. Send ONE call whose arguments are a single JSON object matching the ${name} schema, or finish; after ${MALFORMED_STOP} the turn ends.]`
: "";
this.transcript.push({ role: "tool", tool_call_id: callId, content: complaint + firm });
this.transcript.markMalformedCall(callId);
return { name, result: complaint, auto: true, repeated: false };
}
// Timed around execution only. Waiting on a human to approve a gated
Expand Down Expand Up @@ -5302,7 +5324,12 @@ export class Engine {
preview: hide(capture(result)),
auto: !decision.ask,
};
this.transcript.push({ role: "tool", tool_call_id: callId, content: result });
const firmHere =
refused && this.malformedStreak >= MALFORMED_WARN
? ` [molt: that is ${this.malformedStreak} malformed calls in a row. Send ONE call whose arguments are a single JSON object matching the tool's schema, or finish; after ${MALFORMED_STOP} the turn ends.]`
: "";
this.transcript.push({ role: "tool", tool_call_id: callId, content: result + firmHere });
if (refused) this.transcript.markMalformedCall(callId);
return { name, result, auto: !decision.ask, repeated: repeatedHere };
}

Expand Down Expand Up @@ -5789,7 +5816,7 @@ export class Engine {
* is what it says: verified only if it passed, not proven if it did not,
* unverified when nothing was sealed. Returns whether a bar ran.
*/
async function* judgeOnDisk(why: "deadline" | "provider", claim: string): AsyncGenerator<EngineEvent, boolean> {
async function* judgeOnDisk(why: "deadline" | "provider" | "malformed", claim: string): AsyncGenerator<EngineEvent, boolean> {
self.turnEndedBy = why;
if (opts.ask) return false;
let barThis = barNow();
Expand Down Expand Up @@ -5952,6 +5979,21 @@ export class Engine {
return;
}

// A model that keeps sending malformed calls is stopped like a clock that
// ran out: no more tool calls, the work on disk is judged, and the
// receipt says the model, not the provider, ended the turn.
if (this.malformedStreak >= MALFORMED_STOP) {
log?.append("note", { text: `ended: malformed tool calls (${this.malformedStreak} in a row)`, malformedStop: true });
yield {
kind: "info",
text: `ended: ${this.malformedStreak} malformed tool calls in a row — no more tool calls this turn. The work on disk is judged as it stands.`,
};
this.turnEndedBy = "malformed";
const judged = this.turnWrites.length > 0 ? yield* judgeOnDisk("malformed", "") : false;
if (!judged) yield* this.salvage("You sent several malformed tool calls in a row, so this turn has ended.", fetchFn, log);
return;
}

// An explicit budget speaks for itself, and speaks first: one knob
// should not produce two different messages.
if (this.overBudget()) {
Expand Down
8 changes: 5 additions & 3 deletions src/receipts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type ReceiptRecord = {
*/
ask?: boolean;
/** Set when the model was stopped and the tree judged as it stood. */
endedBy?: "deadline" | "provider";
endedBy?: "deadline" | "provider" | "malformed";
/** True when the cost rests on molt's own token estimate anywhere in the session. */
costEstimated?: boolean;
/**
Expand Down Expand Up @@ -373,7 +373,7 @@ export class Receipts {
/** True for a question: the bar ran advisory and could not refuse. */
ask?: boolean;
/** The model was stopped (clock, provider) and the tree was judged as it stood. */
endedBy?: "deadline" | "provider";
endedBy?: "deadline" | "provider" | "malformed";
/**
* Every file the turn changed, with the hashes that prove it — and which
* lines it wrote, so the receipt can show the work rather than describe it.
Expand Down Expand Up @@ -527,7 +527,9 @@ export class Receipts {
? [
args.endedBy === "deadline"
? "The turn's time budget ran out before the model said it was done; the sealed checks ran on the tree as it stood."
: "The provider failed after work had been done; the sealed checks ran on the tree as it stood.",
: args.endedBy === "malformed"
? "The model sent malformed tool calls several times in a row, so Maat ended the turn; the sealed checks ran on the tree as it stood."
: "The provider failed after work had been done; the sealed checks ran on the tree as it stood.",
"",
]
: []),
Expand Down
51 changes: 46 additions & 5 deletions src/transcript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ export class Transcript {
this.working.push(msg);
}

/**
* Send this call's arguments as an excerpt from now on. A run that made 17
* malformed `act` calls in a row resent every one of them on every step,
* prompts grew to 630k tokens, and one task cost $0.82 for a 181-byte file
* (2026-10-07). The mark lives on the message, so it survives a restore; the
* record and captures keep the full text.
*/
markMalformedCall(id: string): void {
for (let i = this.working.length - 1; i >= 0; i--) {
const m = this.working[i]!;
if (m.role === "assistant" && m.tool_calls?.some((c) => c.id === id)) {
const refused = new Set(m.molt?.refusedCalls ?? []);
refused.add(id);
this.working[i] = { ...m, molt: { ...m.molt, refusedCalls: [...refused] } };
return;
}
}
}

/**
* Fold the tool results for `parts` into one result for the call `id`.
*
Expand Down Expand Up @@ -158,8 +177,14 @@ export class Transcript {
// llama.cpp Qwen failed with a 500. Leading system messages are joined
// in order; a system message anywhere later goes as a user message.
const out: Omit<Msg, "molt">[] = [];
for (const { molt: _molt, ...m } of this.all()) {
if (m.role !== "system") out.push(repair && Array.isArray(m.tool_calls) ? { ...m, tool_calls: m.tool_calls.map(wireCall) } : m);
for (const { molt, ...m } of this.all()) {
const refused = new Set(molt?.refusedCalls ?? []);
if (m.role !== "system")
out.push(
repair && Array.isArray(m.tool_calls)
? { ...m, tool_calls: m.tool_calls.map((c) => (c.id && refused.has(c.id) ? excerptCall(c) : wireCall(c))) }
: m,
);
else if (out.length === 0) out.push({ ...m });
else if (out.length === 1 && out[0]!.role === "system") out[0] = { ...out[0]!, content: `${out[0]!.content ?? ""}\n\n${m.content ?? ""}` };
else out.push({ role: "user", content: m.content ?? "" });
Expand Down Expand Up @@ -556,7 +581,8 @@ export function buildDigest(dropped: Msg[]): string {
} catch {
detail = "(unparseable arguments)";
}
actions.push(`${c.function.name}: ${detail}`);
// Capped: an act whose actions arrived as one huge string printed all of it here.
actions.push(`${c.function.name}: ${cap(detail, 200)}`);
}
}

Expand Down Expand Up @@ -653,11 +679,26 @@ export function wireArgs(text: string): string {
} catch {
/* fall through */
}
return JSON.stringify({ _unparsed: text });
return JSON.stringify({ _unparsed: excerpt(text) });
}
if (isObject(v)) return text;
if (v === null) return "{}";
return JSON.stringify({ _unparsed: text });
return JSON.stringify({ _unparsed: excerpt(text) });
}

/** How much of a refused call's arguments goes back to the provider. */
export const MALFORMED_EXCERPT_CHARS = 300;

/** The start of `text`, and how much was left out. Enough for the model to see its own mistake. */
export function excerpt(text: string, max = MALFORMED_EXCERPT_CHARS): string {
return text.length <= max ? text : `${text.slice(0, max)}…[${text.length - max} more characters not resent]`;
}

/** A refused call as it goes back to the provider: always an object, never the whole text. */
function excerptCall<T extends { function?: { arguments?: unknown } }>(c: T): T {
const a = c.function?.arguments;
const text = typeof a === "string" ? a : JSON.stringify(a ?? {});
return { ...c, function: { ...c.function!, arguments: JSON.stringify({ _refused: excerpt(text) }) } };
}

function wireCall<T extends { function?: { arguments?: unknown } }>(c: T): T {
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export type Msg = {
};

export type MsgMeta = {
/**
* Ids of this assistant message's tool calls that Maat refused as malformed.
* Their arguments go back to the provider as an excerpt (transcript.ts). Kept
* on the message so a restored transcript keeps sending the excerpt.
*/
refusedCalls?: string[];
/** True for a mechanically-generated digest of shed context. */
digest?: true;
/** True for context deliberately re-attached from the archive. */
Expand Down Expand Up @@ -642,7 +648,7 @@ export type EngineEvent =
* happened. The outcome is what the sealed bar said of the tree as it
* stood, never a default.
*/
endedBy?: "deadline" | "provider";
endedBy?: "deadline" | "provider" | "malformed";
/** The turn's wall-clock budget ended it (endedBy "deadline"). */
deadline?: boolean;
/**
Expand Down
Loading
Loading