xonecas.io
64875
Mc

mini-coder / commit 6e09581

feat: doom loop catch

xonecas2026-05-01 6e095813907713198ab5ec3fef381843fa2eec7c parent 726a2be

5 files changed

benchmarks/terminal-bench-suite.sh+1−1
@@ -6,5 +6,5 @@ PYTHONPATH="$PWD" harbor run -y \
66 --model openai-codex/gpt-5.5 \
77 --n-attempts 1 \
88 --n-concurrent 2 \
9- --max-retries 0 \
9+ --max-retries 1 \
1010 --mounts-json '[{"type":"bind","source":"/home/xonecas/.config/mini-coder","target":"/root/.config/mini-coder","bind":{"create_host_path":false}}]'
src/headless.ts+20−2
@@ -1,6 +1,11 @@
11 import type { Message } from "@mariozechner/pi-ai";
22 import { streamAgent } from "./agent";
3-import { buildSystemPrompt, injectEnvReminder, MAIN_PROMPT } from "./prompt";
3+import {
4+ buildSystemPrompt,
5+ injectEnvReminder,
6+ insertToolUsageReminder,
7+ MAIN_PROMPT,
8+} from "./prompt";
49 import { bash, runBashTool } from "./tool-bash";
510 import { edit, runEditTool } from "./tool-edit";
611 import { read, runReadTool } from "./tool-read";
@@ -37,9 +42,22 @@ export async function streamHeadless(
3742 for await (const ev of agent) {
3843 switch (ev.type) {
3944 case "message_end":
40- case "tool_message_end":
4145 console.log(JSON.stringify(ev.message));
4246 break;
47+
48+ case "tool_message_end": {
49+ const withReminder = insertToolUsageReminder(messages, ev.message);
50+ const idx = messages.findIndex(
51+ (m) =>
52+ m.role === "toolResult" && m.toolCallId === withReminder.toolCallId,
53+ );
54+ if (idx >= 0) {
55+ messages[idx] = withReminder;
56+ }
57+
58+ console.log(JSON.stringify(withReminder));
59+ break;
60+ }
4361 }
4462 }
4563
src/prompt.ts+54−33
@@ -2,7 +2,7 @@ import { promises } from "node:fs";
22 import { readdir } from "node:fs/promises";
33 import { homedir, platform } from "node:os";
44 import { join } from "node:path";
5-import type { Message, ToolCall, ToolResultMessage } from "@mariozechner/pi-ai";
5+import type { Message, ToolResultMessage } from "@mariozechner/pi-ai";
66 import simpleGit, { type StatusResult } from "simple-git";
77 import { parseSkillFrontmatter } from "./shared";
88
@@ -211,47 +211,68 @@ export async function injectEnvReminder(): Promise<string> {
211211 return `<system-reminder>\n${envStatus}\n</system-reminder>`;
212212 }
213213
214-// TODO: Needs to be updated since we are deprecating the task tool
215-// for now. Needs to check for similar or identical tool calls, aka
216-// Doom looping.
214+const doomLoopReminder = `<system-reminder>
215+You may be entering a tool-call doom loop: recent tool usage is repetitive or not clearly progressing.
216+
217+- Stop repeating the same or similar tool call unless new evidence requires it.
218+- Re-read the user's request and summarize what is known, what failed, and what is still needed.
219+- Change strategy before calling more tools: narrow the next check, use a different source of evidence, or ask the user if blocked.
220+- If the request is complete, stop calling tools and provide the final answer.
221+</system-reminder>`;
222+
223+// Checks recent tool usage for simple repeated-call patterns and inserts an
224+// anti-doom-loop reminder when the agent appears stuck.
217225 export function insertToolUsageReminder(
218226 messages: Message[],
219227 toolMessage: ToolResultMessage,
220-) {
221- // check for the last 5 tool call assistant messages
222- // if they are non-`task` tool calls insert the reminder
223- // as a prefix.
224- let output = toolMessage.content
225- .filter((b) => b.type === "text")
226- .map((b) => b.text)
227- .join("\n");
228-
229- const budget = 5;
230- const toolCalls: ToolCall[] = [];
231- const lastUserMessageIndex = messages.findLastIndex((m) => m.role === "user");
232- const messagesSinceLastUser = messages.slice(lastUserMessageIndex + 1);
233-
234- messagesSinceLastUser.forEach((m) => {
235- if (m.role === "assistant") {
236- const toolCallsBlocks = m.content.filter((b) => b.type === "toolCall");
237- toolCalls.push(...toolCallsBlocks);
238- }
228+): ToolResultMessage {
229+ const lastReminderIdx = messages.findLastIndex((m) => {
230+ return (
231+ m.role === "toolResult" &&
232+ m.content.find(
233+ (b) => b.type === "text" && b.text.includes(doomLoopReminder),
234+ )
235+ );
239236 });
240- const recentToolCalls = toolCalls.slice(-budget);
241- const taskSeen = recentToolCalls.some((call) => call.name === "task");
237+ const lastUserMessageIdx = messages.findLastIndex((m) => {
238+ return m.role === "user";
239+ });
240+ const messagesSinceLast = messages.slice(
241+ Math.max(lastReminderIdx, lastUserMessageIdx) + 1,
242+ );
243+ const minMessagesForReminder = 4;
242244
243- if (toolCalls.length >= budget && !taskSeen) {
244- output = `<system-reminder>
245-You are currently making repeated individual tool calls. This fragments context and reduces efficiency.
245+ if (messagesSinceLast.length < minMessagesForReminder) return toolMessage;
246246
247-- Stop and plan: consolidate remaining steps into a single **task** tool call.
248-- If the user request is fully completed, stop calling tools and provide your final answer.
249-</system-reminder>
247+ let errorCount = 0;
248+ let sameToolCount = 0;
249+ const seenArgs = new Set<string>();
250250
251-${output}`;
251+ for (const msg of messagesSinceLast) {
252+ if (msg.role === "toolResult" && msg.isError) errorCount++;
253+
254+ if (msg.role === "assistant") {
255+ const calls = msg.content.filter((b) => b.type === "toolCall");
256+ for (const c of calls) {
257+ const args = JSON.stringify(c.arguments);
258+ if (seenArgs.has(args)) sameToolCount++;
259+ seenArgs.add(args)
260+ }
261+ }
252262 }
253263
254- toolMessage.content = [{ type: "text", text: output }];
264+ if (
265+ errorCount >= minMessagesForReminder ||
266+ sameToolCount >= minMessagesForReminder
267+ ) {
268+ return {
269+ ...toolMessage,
270+ content: [
271+ ...toolMessage.content,
272+ { type: "text", text: doomLoopReminder },
273+ ],
274+ };
275+ }
255276
256277 return toolMessage;
257278 }
src/tui-components.ts+4−4
@@ -114,11 +114,11 @@ export function ContextPill(state: TUIState) {
114114 const smartPercent = Math.floor((state.contextSize / smartMax) * 100);
115115 if (smartPercent > 90) {
116116 bg = theme.bred;
117- } else if (smartPercent > 80) {
117+ } else if (smartPercent > 85) {
118118 bg = theme.red;
119- } else if (smartPercent > 70) {
119+ } else if (smartPercent > 80) {
120120 bg = theme.yellow;
121- } else if (smartPercent > 50) {
121+ } else if (smartPercent > 60) {
122122 bg = theme.byellow;
123123 }
124124 text = `~${percent}%`;
@@ -126,7 +126,7 @@ export function ContextPill(state: TUIState) {
126126
127127 text += ` (${state.options.model.contextWindow / 1000}k)`;
128128
129- return TextPill(text, theme.bblack, bg);
129+ return TextPill(text, theme.black, bg);
130130 }
131131
132132 export function GitPill(state: TUIState) {
src/tui.ts+19−15
@@ -1,7 +1,12 @@
11 import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
22 import simpleGit from "simple-git";
33 import { compactContext, streamAgent } from "./agent";
4-import { buildSystemPrompt, injectEnvReminder, MAIN_PROMPT } from "./prompt";
4+import {
5+ buildSystemPrompt,
6+ injectEnvReminder,
7+ insertToolUsageReminder,
8+ MAIN_PROMPT,
9+} from "./prompt";
510 import { updateSession } from "./session";
611 import { estimateTokens, secureRandomString } from "./shared";
712 import { bash, runBashTool } from "./tool-bash";
@@ -184,20 +189,19 @@ async function streamAgentTUI(state: TUIState) {
184189 break;
185190
186191 case "tool_message_end": {
187- // TODO: reminder needs to be refactored
188- // const withReminder = insertToolUsageReminder(
189- // state.messages,
190- // ev.message,
191- // );
192-
193- // const idx = state.messages.findIndex(
194- // (m) =>
195- // m.role === "toolResult" &&
196- // m.toolCallId === withReminder.toolCallId,
197- // );
198- // if (idx >= 0) {
199- // state.messages[idx] = withReminder;
200- // }
192+ const withReminder = insertToolUsageReminder(
193+ state.messages,
194+ ev.message,
195+ );
196+
197+ const idx = state.messages.findIndex(
198+ (m) =>
199+ m.role === "toolResult" &&
200+ m.toolCallId === withReminder.toolCallId,
201+ );
202+ if (idx >= 0) {
203+ state.messages[idx] = withReminder;
204+ }
201205
202206 state.contextSize = estimateTokens(JSON.stringify(ctx));
203207 }