xonecas.io
64875
Mc

mini-coder / commit c62c160

feat: tool usage reminders

xonecas2026-04-26 c62c160a3bb5358a60f3842553729891526ee793 parent 0b937bf

4 files changed

AGENTS.md+5−5
@@ -1,14 +1,12 @@
11 # Mini coder next
22
3-A new version of mini-coder, written from the ground up as library first, plugins.
3+A new version of mini-coder, written from the ground up as library first.
44 Version `mini-coder@0.5.x` is currently stable and mostly complete, that's our reference implementation.
5-Benchmarking from the start, keep the agent dumb, build the library-first architecture, then
6-run an automated benchmarking loop to shape/hone the harness.
75
86 ## Dog-fooding
97
10-Be aware that you are mini-coder, running the dev version of this repository while editing it.
11-We are dog-fooding the code: changes to prompts, tools, context building, streaming, or runtime behavior may affect the agent we are actively using during the same development session.
8+if this is in your context window, you are using the current repo's harness `mini-coder`. Be aware of this when
9+debugging and testing live with your user.
1210
1311 ## Core dependencies
1412
@@ -46,4 +44,6 @@ Plain text readme file: https://raw.githubusercontent.com/badlogic/pi-mono/refs/
4644
4745 Then lint with: `bunx biome check` and `tsc --noEmit`.
4846
47+**Do not use the `package.json` commands/scripts they are for the users.** Use the commands above instead.
48+
4949 > Always fix any warnings or errors even if they were not introduced by you, prefer non destructive fixes.
src/agent.ts+54−5
@@ -7,6 +7,7 @@ import {
77 type Context,
88 type Message,
99 streamSimple,
10+ type ToolCall,
1011 type ToolResultMessage,
1112 } from "@mariozechner/pi-ai";
1213 import { type StatusResult, simpleGit } from "simple-git";
@@ -194,6 +195,51 @@ export async function buildSystemPrompt(systemPrompt: string) {
194195 return complete;
195196 }
196197
198+export function insertToolUsageReminder(
199+ messages: Message[],
200+ toolMessage: ToolResultMessage,
201+) {
202+ // check for the last 5 tool call assistant messages
203+ // if they are non-`task` tool calls insert the reminder
204+ // as a prefix.
205+ let output = toolMessage.content
206+ .filter((b) => b.type === "text")
207+ .map((b) => b.text)
208+ .join("\n");
209+
210+ const budget = 5;
211+ const toolCalls: ToolCall[] = [];
212+ messages.forEach((m) => {
213+ if (m.role === "assistant") {
214+ const toolCalls = m.content.filter((b) => b.type === "toolCall");
215+ toolCalls.push(...toolCalls);
216+ }
217+ });
218+
219+ let taskSeen = 0;
220+ let idx = toolCalls.length;
221+
222+ while (idx--) {
223+ const name = toolCalls[idx].name;
224+ if (name === "task") taskSeen++;
225+ if (idx > budget && taskSeen < 1) break;
226+ }
227+
228+ if (taskSeen < 1) {
229+ output = `# System reminder:
230+
231+ > You are in a tool loop. Stop and use the task tool instead.
232+
233+ ---
234+
235+ ${output}`;
236+ }
237+
238+ toolMessage.content = [{ type: "text", text: output }];
239+
240+ return toolMessage;
241+}
242+
197243 export async function streamAgent(
198244 apiKey: string,
199245 tools: ToolAndRunner[],
@@ -202,7 +248,7 @@ export async function streamAgent(
202248 options: CliOptions,
203249 abortController?: AbortController,
204250 streamFn?: (ev: AssistantMessageEvent, context: Context) => void,
205- toolsFn?: (tool: ToolResultMessage, context: Context) => void,
251+ toolsFn?: (tool: ToolResultMessage, context: Context) => ToolResultMessage,
206252 completeFn?: (msg: AssistantMessage, context: Context) => void,
207253 ) {
208254 const context: Context = {
@@ -266,7 +312,7 @@ export async function streamAgent(
266312 output = `Unknown tool: ${call.name}`;
267313 }
268314
269- const msg: ToolResultMessage = {
315+ let msg: ToolResultMessage = {
270316 role: "toolResult",
271317 toolCallId: call.id,
272318 toolName: call.name,
@@ -274,10 +320,13 @@ export async function streamAgent(
274320 isError,
275321 timestamp: Date.now(),
276322 };
277- context.messages.push(msg);
278323
279- // Update with tool results.
280- toolsFn?.(msg, context);
324+ // Update with tool results. let the user make changes if they want
325+ if (toolsFn) {
326+ msg = toolsFn(msg, context);
327+ }
328+
329+ context.messages.push(msg);
281330 }
282331 }
283332 } finally {
src/headless.ts+2−0
@@ -71,6 +71,8 @@ export async function streamHeadless(
7171 msg: `> ${tool.toolName} output:\n---\n${text}\n---`,
7272 json: JSON.stringify(tool),
7373 });
74+
75+ return tool;
7476 };
7577
7678 const onComplete = (msg: AssistantMessage) => {
src/tui.ts+3−1
@@ -7,7 +7,7 @@ import type {
77 Message,
88 ToolResultMessage,
99 } from "@mariozechner/pi-ai";
10-import { MAIN_PROMPT, streamAgent } from "./agent";
10+import { insertToolUsageReminder, MAIN_PROMPT, streamAgent } from "./agent";
1111 import { getApiKey } from "./oauth";
1212 import { estimateTokens } from "./shared";
1313 import { bash, runBashTool } from "./tool-bash";
@@ -169,8 +169,10 @@ export async function streamTUI(state: TUIState) {
169169 };
170170
171171 const onTool = (tool: ToolResultMessage, ctx: Context) => {
172+ tool = insertToolUsageReminder(state.messages, tool);
172173 state.messages.push(tool);
173174 state.contextSize = estimateTokens(JSON.stringify(ctx));
175+ return tool;
174176 };
175177
176178 const onComplete = (msg: AssistantMessage, ctx: Context) => {