@@ -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 }