mini-coder 0.5.14 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +26 -109
  2. package/bin/mc.ts +8 -11
  3. package/bun.lock +79 -269
  4. package/nono-mini-coder.json +42 -0
  5. package/package.json +17 -22
  6. package/src/agent.ts +243 -1403
  7. package/src/args.ts +289 -0
  8. package/src/headless.ts +41 -359
  9. package/src/index.ts +29 -1016
  10. package/src/oauth.ts +117 -0
  11. package/src/prompt.ts +219 -284
  12. package/src/session.ts +55 -1306
  13. package/src/shared.ts +117 -38
  14. package/src/tool-bash.ts +110 -0
  15. package/src/tool-edit.ts +133 -0
  16. package/src/tool-read.ts +80 -293
  17. package/src/tui-components.ts +150 -0
  18. package/src/tui-conversation.ts +271 -0
  19. package/src/tui-editor.ts +29 -0
  20. package/src/tui-overlay.ts +403 -0
  21. package/src/tui.ts +228 -0
  22. package/src/types.ts +164 -0
  23. package/tsconfig.json +17 -0
  24. package/BENCHMARK.md +0 -107
  25. package/LICENSE +0 -9
  26. package/PROGRESS.md +0 -5
  27. package/assets/icon-1-minimal.svg +0 -31
  28. package/assets/icon-2-dark-terminal.svg +0 -48
  29. package/assets/icon-3-gradient-modern.svg +0 -45
  30. package/assets/icon-4-filled-bold.svg +0 -54
  31. package/assets/icon-5-community-badge.svg +0 -63
  32. package/assets/mc-claude-smart.png +0 -0
  33. package/assets/mc-gpt-smart.png +0 -0
  34. package/assets/preview-0-5-0.png +0 -0
  35. package/assets/preview.gif +0 -0
  36. package/benchmark-baseline.sh +0 -15
  37. package/benchmark-loop.sh +0 -19
  38. package/skills-lock.json +0 -15
  39. package/src/assistant-output.ts +0 -73
  40. package/src/cli.ts +0 -134
  41. package/src/delegation.ts +0 -238
  42. package/src/errors.ts +0 -15
  43. package/src/git.ts +0 -247
  44. package/src/input.ts +0 -168
  45. package/src/mcp.ts +0 -609
  46. package/src/paths.ts +0 -37
  47. package/src/session-message.ts +0 -385
  48. package/src/settings.ts +0 -449
  49. package/src/skills.ts +0 -271
  50. package/src/submit.ts +0 -376
  51. package/src/text.ts +0 -71
  52. package/src/theme.ts +0 -330
  53. package/src/tool-common.ts +0 -93
  54. package/src/tool-delegate.ts +0 -125
  55. package/src/tool-grep.ts +0 -606
  56. package/src/tool-shell.ts +0 -1051
  57. package/src/tools.ts +0 -1179
  58. package/src/ui/agent.ts +0 -320
  59. package/src/ui/commands.test.ts +0 -957
  60. package/src/ui/commands.ts +0 -848
  61. package/src/ui/conversation.test.ts +0 -585
  62. package/src/ui/conversation.ts +0 -1836
  63. package/src/ui/help.ts +0 -158
  64. package/src/ui/input.test.ts +0 -64
  65. package/src/ui/input.ts +0 -138
  66. package/src/ui/overlay.ts +0 -59
  67. package/src/ui/runtime.ts +0 -69
  68. package/src/ui/status.ts +0 -220
  69. package/src/ui.ts +0 -1190
  70. package/src/version.ts +0 -48
package/src/oauth.ts ADDED
@@ -0,0 +1,117 @@
1
+ import readline from "node:readline";
2
+
3
+ import { getEnvApiKey, getProviders } from "@mariozechner/pi-ai";
4
+ import {
5
+ getOAuthApiKey,
6
+ getOAuthProvider,
7
+ getOAuthProviders,
8
+ type OAuthProviderId,
9
+ } from "@mariozechner/pi-ai/oauth";
10
+ import { AUTH_PATH as AUTH_FILE } from "./shared";
11
+ import type { CliOptions, SavedOAuthCreds } from "./types";
12
+
13
+ export function isOAuthProvider(provider: string): boolean {
14
+ return getOAuthProviders().some(
15
+ (oauthProvider) => oauthProvider.id === provider,
16
+ );
17
+ }
18
+
19
+ export async function getAvailableProviders(): Promise<string[]> {
20
+ const auth = await readCreds();
21
+ const loggedInOAuthProviders = getOAuthProviders()
22
+ .map((provider) => provider.id)
23
+ .filter((provider) => auth[provider]);
24
+ const envKeyProviders = getProviders().filter(
25
+ (provider) => !!getEnvApiKey(provider),
26
+ );
27
+ const providers: string[] = [];
28
+ const providerIds = new Set<string>();
29
+
30
+ for (const provider of [...loggedInOAuthProviders, ...envKeyProviders]) {
31
+ if (providerIds.has(provider)) continue;
32
+
33
+ providers.push(provider);
34
+ providerIds.add(provider);
35
+ }
36
+
37
+ return providers;
38
+ }
39
+
40
+ export async function loginOAuth(provider: OAuthProviderId) {
41
+ const oauthProvider = getOAuthProvider(provider);
42
+ if (!oauthProvider) throw new Error(`Unknown OAuth provider: ${provider}`);
43
+
44
+ const rl = readline.createInterface({
45
+ input: process.stdin,
46
+ output: process.stdout,
47
+ });
48
+
49
+ try {
50
+ const creds = await oauthProvider.login({
51
+ onAuth: ({ url, instructions }) => {
52
+ console.log(`Open: ${url}`);
53
+ if (instructions) console.log(instructions);
54
+ },
55
+ onPrompt: async (prompt) => {
56
+ let answer: string = "";
57
+ await rl.question(prompt.message, (a) => (answer = a));
58
+ return answer;
59
+ },
60
+ onProgress: (message) => console.log(message),
61
+ });
62
+
63
+ await writeCreds({ [provider]: { type: "oauth", ...creds } });
64
+ } finally {
65
+ rl.close();
66
+ }
67
+
68
+ return await readCreds();
69
+ }
70
+
71
+ export async function getApiKey(options: CliOptions) {
72
+ const provider = options.model.provider;
73
+ const auth = await readCreds();
74
+
75
+ if (isOAuthProvider(provider)) {
76
+ const result = await getOAuthApiKey(provider, auth);
77
+ if (result) {
78
+ auth[provider] = { type: "oauth", ...result.newCredentials };
79
+ await writeCreds(auth);
80
+
81
+ return result.apiKey;
82
+ }
83
+ }
84
+
85
+ const envApiKey = getEnvApiKey(provider);
86
+ if (envApiKey) return envApiKey;
87
+
88
+ const knownProviders = getProviders() as string[];
89
+ if (!knownProviders.includes(provider)) {
90
+ if (options.model.api === "openai-completions") {
91
+ // pi-ai requires a truthy apiKey for OpenAI-compatible local providers like Ollama.
92
+ return "dummy";
93
+ }
94
+
95
+ return undefined;
96
+ }
97
+
98
+ throw new Error("Not logged in");
99
+ }
100
+
101
+ export async function readCreds(): Promise<SavedOAuthCreds> {
102
+ const file = Bun.file(AUTH_FILE);
103
+ if (await file.exists()) {
104
+ return JSON.parse(await file.text());
105
+ }
106
+
107
+ return {};
108
+ }
109
+
110
+ async function writeCreds(creds: SavedOAuthCreds) {
111
+ await Bun.write(AUTH_FILE, JSON.stringify(await mergeCreds(creds)));
112
+ }
113
+
114
+ async function mergeCreds(newCreds: SavedOAuthCreds) {
115
+ const oldCreds = await readCreds();
116
+ return { ...oldCreds, ...newCreds };
117
+ }
package/src/prompt.ts CHANGED
@@ -1,322 +1,257 @@
1
- /**
2
- * System prompt construction.
3
- *
4
- * Assembles the full system prompt from the core prompt template plus
5
- * dynamic context: AGENTS.md files, the skill catalog, and the current
6
- * environment block.
7
- *
8
- * @module
9
- */
10
-
11
- import { existsSync, readFileSync } from "node:fs";
12
- import { dirname, join, relative } from "node:path";
13
- import type { GitState } from "./git.ts";
14
- import { canonicalizePath } from "./paths.ts";
15
- import { buildSkillCatalog, type Skill } from "./skills.ts";
16
-
17
- // ---------------------------------------------------------------------------
18
- // Types
19
- // ---------------------------------------------------------------------------
20
-
21
- /** A discovered AGENTS.md file with its content. */
22
- export interface AgentsMdFile {
23
- /** Absolute path to the file. */
24
- path: string;
25
- /** Raw file content. */
26
- content: string;
27
- }
28
-
29
- /** Options for building the system prompt. */
30
- interface BuildSystemPromptOpts {
31
- /** Current working directory. */
32
- cwd: string;
33
- /** Active provider/model identifier. */
34
- modelLabel: string;
35
- /** Normalized host OS label (`linux`, `mac`, or `docker`). */
36
- os: string;
37
- /** Active shell name (for example `bash` or `zsh`). */
38
- shell: string;
39
- /** Whether the active model supports image input. */
40
- supportsImages?: boolean;
41
- /** Git repository state, or `null`/`undefined` if not in a repo. */
42
- git?: GitState | null;
43
- /** Discovered AGENTS.md files, ordered root-to-leaf. */
44
- agentsMd?: AgentsMdFile[];
45
- /** Discovered agent skills. */
46
- skills?: Skill[];
1
+ import { promises } from "node:fs";
2
+ import { readdir } from "node:fs/promises";
3
+ import { homedir, platform } from "node:os";
4
+ import { join } from "node:path";
5
+ import type { Message, ToolCall, ToolResultMessage } from "@mariozechner/pi-ai";
6
+ import simpleGit, { type StatusResult } from "simple-git";
7
+ import { parseSkillFrontmatter } from "./shared";
8
+
9
+ export const MAIN_PROMPT = `# You are "mini-coder", a coding agent.
10
+
11
+ IMPORTANT: Be defensive with existing changes and destructive commands.
12
+ IMPORTANT: Do not overstate what changed or what was verified. Summaries must match the diff.
13
+
14
+ ## Role
15
+ You help users by reading files, executing commands, editing code, and writing new files. Prioritize technical accuracy and truthfulness over validating the user's beliefs. Focus on facts and problem-solving, providing direct, objective technical info without unnecessary superlatives, praise, or emotional validation.
16
+
17
+ <example>
18
+ When referencing specific functions or pieces of code, include the pattern \`file_path:line_number\`.
19
+ For example: "Clients are handled in the \`connectToServer\` function in src/services/process.ts:712."
20
+ </example>
21
+
22
+ User messages and Tool results may include <system-reminder> tags. These contain system-generated reminders and bear no direct relation to the specific tool result in which they appear.
23
+
24
+ ## Tools
25
+ - You have access to bash, read and edit tools. Prefer using read and edit for file operations, use bash for finding read candidates or to run development commands.
26
+
27
+ <example>
28
+ > User: please read the README.md and add rich code examples.
29
+
30
+ - Use the bash tool to find the path for README.md, prefer "ls" or "fd/find", and "rg/grep".
31
+ - Then read the file with the read tool to find the replacement areas and mathcing patterns
32
+ - Edit the file using the edit tool. Review the output diff, use the read tool again to verify if needed.
33
+ - Reply to the user that the edit was done.
34
+ </example>
35
+
36
+ ## Workflow
37
+ - Stay rooted on the user's request. Don't wander into tangents or explore out of curiosity.
38
+ - Gather only the information needed to fulfill the request, then stop exploring and complete it.
39
+ - Narrate your edits with brief commentary during long tasks so the user can follow progress.
40
+ - Verify your changes via compilation, tests, or manual checks whenever possible.
41
+
42
+ ## Tone
43
+ - Be concise. Use a professional colleague tone: direct, never condescending, and never rude.
44
+
45
+ ## Error Handling
46
+ - If a tool call fails or is denied, do NOT re-attempt the exact same call. Analyze why it failed and adjust your approach.
47
+
48
+ ## Safety rules
49
+ - Answer all user requests without guessing, or assuming. Verify your answers and claims before making them.
50
+ - Use recent online information, the current environment, and your training data combined for a complete answer.
51
+ - Ensure that you fulfill the user's expectation, requirements and contract **exactly**.
52
+ - Be defensive with existing changes and destructive commands, they could harm your user's changes.
53
+ - Use temp directory for temp files, scripts, plan files, or anything that doesn't match the requested output.
54
+ - Do not over-scope your work, or add more scope during implementation.
55
+ - Avoid over-enginnering, hacks or creative solutions. The boring, simple and repliable is always preferred.
56
+ - Do not overstate what changed or what was verified. Summaries must match the diff.
57
+
58
+ IMPORTANT: Never guess or assume. Verify claims before making them.
59
+ IMPORTANT: Do not over-scope work or add scope during implementation.
60
+ `;
61
+
62
+ async function getDir() {
63
+ const ignoreFile = Bun.file(".gitignore");
64
+ let ignoreContent = "";
65
+ if (await ignoreFile.exists()) {
66
+ ignoreContent = await ignoreFile.text();
67
+ }
68
+ const ignored = ignoreContent.split("\n");
69
+ const dir = [];
70
+ const glob = promises.glob(["*", "*/*"], { exclude: ignored });
71
+ for await (const file of glob) {
72
+ dir.push(file);
73
+ }
74
+ return dir;
47
75
  }
48
76
 
49
- // ---------------------------------------------------------------------------
50
- // AGENTS.md discovery
51
- // ---------------------------------------------------------------------------
52
-
53
- /** File name to look for during the AGENTS.md walk. */
54
- const AGENT_FILENAME = "AGENTS.md";
55
-
56
- /** Resolve the AGENTS.md scan root from git/home/env inputs. */
57
- export function resolveAgentsScanRoot(
58
- _cwd: string,
59
- gitRoot: string | null,
60
- homeDir: string,
61
- agentsRootEnv = process.env.MC_AGENTS_ROOT,
62
- ): string {
63
- if (gitRoot) {
64
- return canonicalizePath(gitRoot);
77
+ async function getEnvPrompt() {
78
+ // TODO: What else do the agents always check before answering every time?
79
+ let gitStatus: StatusResult | { nogit: string };
80
+ try {
81
+ gitStatus = await simpleGit().status();
82
+ } catch (_) {
83
+ gitStatus = { nogit: "No git repo in this folder." };
65
84
  }
66
- if (agentsRootEnv === "/") {
67
- return canonicalizePath("/");
85
+ const envKeys = ["PATH", "USER", "LANG", "HOME", "SHELL", "BUN_INSTALL"];
86
+ const env: Record<string, string> = {};
87
+ for (const key of envKeys) {
88
+ const v = Bun.env[key];
89
+
90
+ if (v !== undefined) {
91
+ env[key] = v;
92
+ }
68
93
  }
69
- return canonicalizePath(homeDir);
70
- }
71
94
 
72
- function isWithinScanRoot(path: string, scanRoot: string): boolean {
73
- const relativePath = relative(scanRoot, path);
74
- return (
75
- relativePath === "" ||
76
- (!relativePath.startsWith("..") && relativePath !== "..")
95
+ const envStatus = JSON.stringify(
96
+ {
97
+ os: platform(),
98
+ env,
99
+ cwd: process.cwd(),
100
+ dir: await getDir(),
101
+ git: gitStatus,
102
+ },
103
+ null,
104
+ 4,
77
105
  );
78
- }
79
106
 
80
- function collectAgentsSearchDirs(start: string, root: string): string[] {
81
- if (!isWithinScanRoot(start, root)) {
82
- return [start];
83
- }
107
+ const text = `### Environment status and information
84
108
 
85
- const dirs: string[] = [];
86
- let current = start;
87
- while (true) {
88
- dirs.push(current);
89
- if (current === root) {
90
- return dirs.reverse();
91
- }
109
+ \`\`\`json
110
+ ${envStatus}
111
+ \`\`\`
112
+ `;
92
113
 
93
- const parent = dirname(current);
94
- if (parent === current) {
95
- return dirs.reverse();
96
- }
97
- current = parent;
98
- }
114
+ return text;
99
115
  }
100
116
 
101
- function readAgentsMdFile(dir: string): AgentsMdFile | null {
102
- const filePath = join(dir, AGENT_FILENAME);
103
- if (!existsSync(filePath)) {
104
- return null;
117
+ // `AGENTS.md` support: find it in current folder (./AGENTS.md) and a global one. (`.agents/AGENTS.md`)
118
+ export async function getAGENTSFiles() {
119
+ const content: string[] = [];
120
+
121
+ const globalPath = join(homedir(), ".agents/AGENTS.md");
122
+ const globalFile = Bun.file(globalPath);
123
+
124
+ if (await globalFile.exists()) {
125
+ content.push(await globalFile.text());
105
126
  }
106
127
 
107
- try {
108
- return {
109
- path: filePath,
110
- content: readFileSync(filePath, "utf-8"),
111
- };
112
- } catch {
113
- return null;
128
+ const localPath = join(process.cwd(), "AGENTS.md");
129
+ const localFile = Bun.file(localPath);
130
+
131
+ if (await localFile.exists()) {
132
+ content.push(await localFile.text());
114
133
  }
134
+
135
+ return content.join("\n\n").trim();
115
136
  }
116
137
 
117
- /**
118
- * Walk from `cwd` up to `scanRoot`, collecting AGENTS.md files.
119
- *
120
- * Also checks `globalAgentsDir` for global agent instructions when provided.
121
- * Results are ordered root-to-leaf (general → specific), with global
122
- * instructions first when present.
123
- *
124
- * @param cwd - Starting directory for the walk.
125
- * @param scanRoot - Uppermost directory to include in the walk.
126
- * @param globalAgentsDir - Optional directory for global agent instructions (e.g. `~/.agents/`).
127
- * @returns Array of {@link AgentsMdFile} records, ordered general → specific.
128
- */
129
- export function discoverAgentsMd(
130
- cwd: string,
131
- scanRoot: string,
132
- globalAgentsDir?: string,
133
- ): AgentsMdFile[] {
134
- const root = canonicalizePath(scanRoot);
135
- const start = canonicalizePath(cwd);
136
- const files: AgentsMdFile[] = [];
137
-
138
- if (globalAgentsDir) {
139
- const globalFile = readAgentsMdFile(globalAgentsDir);
140
- if (globalFile) {
141
- files.push(globalFile);
138
+ // `SKILLS.md` discovery from [~|.]/agents/skills/*/SKILL.md
139
+ export async function getSkills(): Promise<string> {
140
+ let skillsBlock: string = "";
141
+ const skillRoots = [
142
+ join(homedir(), ".agents", "skills"),
143
+ join(process.cwd(), ".agents", "skills"),
144
+ ];
145
+
146
+ for (const root of skillRoots) {
147
+ let entries: string[];
148
+
149
+ try {
150
+ entries = await readdir(root);
151
+ } catch {
152
+ continue;
142
153
  }
143
- }
144
154
 
145
- for (const dir of collectAgentsSearchDirs(start, root)) {
146
- const agentsFile = readAgentsMdFile(dir);
147
- if (agentsFile) {
148
- files.push(agentsFile);
155
+ for (const entry of entries) {
156
+ const path = join(root, entry, "SKILL.md");
157
+ const file = Bun.file(path);
158
+
159
+ if (!(await file.exists())) {
160
+ continue;
161
+ }
162
+
163
+ const parsed = parseSkillFrontmatter(await file.text());
164
+
165
+ if (!parsed) {
166
+ continue;
167
+ }
168
+
169
+ skillsBlock += `## ${parsed.name}
170
+
171
+ > Absolute file path to read: ${path}
172
+
173
+ ${parsed.description}
174
+
175
+ `;
149
176
  }
150
177
  }
151
178
 
152
- return files;
153
- }
179
+ if (!skillsBlock.length) return "";
154
180
 
155
- // ---------------------------------------------------------------------------
156
- // Git line formatting
157
- // ---------------------------------------------------------------------------
158
-
159
- /**
160
- * Format a git state snapshot into a single-line string for the environment block.
161
- *
162
- * Fields are omitted when their values are zero. The git line format:
163
- * `Git: branch main | 3 staged, 1 modified, 2 untracked | +5 −2 vs origin/main`
164
- * where the trailing upstream label reflects the repository's actual tracking ref.
165
- *
166
- * @param state - The git state to format.
167
- * @returns Formatted git status line.
168
- */
169
- export function formatGitLine(state: GitState): string {
170
- const parts: string[] = [`Git: branch ${state.branch}`];
171
-
172
- // Working tree counts
173
- const counts: string[] = [];
174
- if (state.staged > 0) counts.push(`${state.staged} staged`);
175
- if (state.modified > 0) counts.push(`${state.modified} modified`);
176
- if (state.untracked > 0) counts.push(`${state.untracked} untracked`);
177
- if (counts.length > 0) parts.push(counts.join(", "));
178
-
179
- // Ahead/behind
180
- if (state.ahead > 0 || state.behind > 0) {
181
- const ab: string[] = [];
182
- if (state.ahead > 0) ab.push(`+${state.ahead}`);
183
- if (state.behind > 0) ab.push(`\u2212${state.behind}`);
184
- const upstream = state.upstream ? ` vs ${state.upstream}` : "";
185
- parts.push(`${ab.join(" ")}${upstream}`);
186
- }
181
+ const skills = `# Skills
182
+
183
+ - The following skills provide specialized instructions for specific tasks.
184
+ - Use the bash tool to read a skill's file when the task matches its description.
185
+ - Use the skill provided absolute file path instead of guessing or constructing one.
186
+ - Skills can be global (in ~/.agents/skills) or local to the directory (./agents/skills)
187
187
 
188
- return parts.join(" | ");
188
+ ${skillsBlock}`;
189
+
190
+ return skills.trim();
189
191
  }
190
192
 
191
- // ---------------------------------------------------------------------------
192
- // Core prompt template
193
- // ---------------------------------------------------------------------------
194
-
195
- function buildCorePrompt(opts: BuildSystemPromptOpts): string {
196
- const lines = [
197
- "You are mini-coder, the best software engineering assistant in the world.",
198
- "",
199
- "The current environment is:",
200
- `- LLM in use: ${opts.modelLabel}`,
201
- `- OS: ${opts.os}`,
202
- `- Current working directory: ${opts.cwd}`,
203
- ];
193
+ export async function buildSystemPrompt(systemPrompt: string) {
194
+ const agentsContent = await getAGENTSFiles();
195
+ const skillsContent = await getSkills();
196
+ let complete = systemPrompt;
204
197
 
205
- if (opts.git) {
206
- lines.push(`- ${formatGitLine(opts.git)}`);
198
+ if (skillsContent) {
199
+ complete += `\n${skillsContent}`;
207
200
  }
208
201
 
209
- lines.push(
210
- `- Shell: ${opts.shell}. Use \`command -v <name>\` to check what is available to you; do not assume environment support.`,
211
- "- Read: Read a text file from disk with offset/limit support.",
212
- "- Grep: Search file contents with ripgrep-style options and structured results.",
213
- "- Edit: Safe exact-text replacement in a single file.",
214
- );
215
-
216
- if (opts.supportsImages) {
217
- lines.push("- Read Image: Read an image from disk.");
202
+ if (agentsContent) {
203
+ complete += `\n${agentsContent}`;
218
204
  }
219
205
 
220
- lines.push(
221
- "",
222
- "## Core working style:",
223
- "",
224
- "- Be concise, direct, and useful.",
225
- "- Use a casual, solution-oriented technical tone. Avoid fluff and performative apologies.",
226
- "- When the user gives a clear command, do it without adding extra work they did not ask for.",
227
- "- Prefer the minimal implementation that satisfies the request exactly.",
228
- "- Use YAGNI. Avoid speculative abstractions, future-proofing, and unnecessary compatibility shims.",
229
- "- Preserve working behavior where possible. Prefer targeted fixes over rewrites.",
230
- "- Be thorough, use fresh eyes and internal analysis before taking action.",
231
- "- Make informed decisions based on the available information and best practices.",
232
- "- Always verify the result of your actions.",
233
- "",
234
- "### Using the shell tool:",
235
- "",
236
- "- Always execute shell commands in non-interactive mode.",
237
- "- Use the appropriate commands and package managers for the specified operating system.",
238
- "- Don't assume the environment supports all commands; check before using them.",
239
- "- Avoid destructive commands that can discard changes or override edits.",
240
- "",
241
- "### Choosing tools:",
242
- "",
243
- "- Prefer `read` for reading file contents instead of `cat`, `sed`, `head`, or `tail`.",
244
- "- Prefer `grep` for content search instead of raw `grep` / `rg`.",
245
- "- Use shell `ls` and `fd` for lightweight exploration when you just need to inspect directories or discover candidate files.",
246
- "",
247
- "### Working with code:",
248
- "",
249
- "- Describe changes before implementing them",
250
- "- Prefer boring dependable solutions over clever ones",
251
- "- Avoid creating extra files, systems or documentation outside of what was asked.",
252
- "- Check requirements, and plan your changes before editing code.",
253
- "- Implement the necessary changes, following good practices and proper error handling.",
254
- "- Prefer the smallest path that leaves the requested end state already true; do not stop at helper scripts, instructions, or half-finished setup when the user asked for the live result itself.",
255
- "- Always verify your changes using compilation, testing, and manual verification when possible.",
256
- "- Before you finish, re-check the explicit deliverables and current state. If the user named files, paths, ports, services, commands, or output values, make sure they already exist and work now.",
257
- "- If the request includes structural constraints on files or outputs (for example allowed commands, required lines, exact formats, or counts), treat those as acceptance criteria too and verify them directly against what you produced, not just through downstream behavior.",
258
- "- Treat concrete command sequences and expected outputs in the user's request as acceptance criteria for the end state. If you verify that flow during the task, do not roll the environment back afterward unless the user explicitly asked for a reset.",
259
- "- If a check or tool result contradicts your expectation, trust the evidence and resolve the mismatch before you answer.",
260
- "- When multiple outputs or end states seem plausible, do not guess or swap in a cleaner alternative after verification. Run the smallest check that distinguishes them, and if you change the state later, verify again.",
261
- "- When verifying with build or test commands, avoid leaving generated binaries or scratch artifacts in the requested output location; use temporary paths or remove them before finishing.",
262
- "- Do not leave helpers, tests, or any other form of temporary files; clean up after yourself and leave no trace.",
263
- "- Ensure you match the requested output exactly. This applies to file names, directory structure, number of files, output formats, and all other details.",
264
- '- "Polish" is not optional; it counts just as much as solving the task.',
265
- "",
266
- "### Task management",
267
- "",
268
- "- Use `todoWrite` proactively for multi-step or non-trivial tasks.",
269
- "- Capture new requirements in the todo list as soon as you understand them.",
270
- "- Use `todoRead` when you need to inspect the current list before updating it or when the user asks for the current plan/status.",
271
- "- Keep the todo list up-to-date above all; mark tasks `in_progress` before starting them and `completed` as soon as verification succeeds.",
272
- "- A todo item is only complete if the requested work is actually finished and verified to the degree the task requires.",
273
- "- Use `cancelled` to remove tasks that are no longer relevant.",
274
- "- Skip todo tools for single trivial tasks and purely conversational/informational requests.",
275
- "- Use the `delegate` tool for bounded subtasks when another focused agent pass would help.",
276
- "- Prefer `delegate` over shelling out to `mc -p` unless you specifically need to exercise the CLI itself.",
277
- "- Do not re-delegate the whole task, spin on repeated self-review prompts, or ask a delegated child to delegate again.",
278
- "- Delegate when you are orchestrating a large to-do/plan execution.",
279
- "",
280
- );
206
+ return complete;
207
+ }
281
208
 
282
- return lines.join("\n");
209
+ export async function injectEnvReminder(): Promise<string> {
210
+ const envStatus = await getEnvPrompt();
211
+ return `<system-reminder>\n${envStatus}\n</system-reminder>`;
283
212
  }
284
213
 
285
- // ---------------------------------------------------------------------------
286
- // System prompt assembly
287
- // ---------------------------------------------------------------------------
288
-
289
- /**
290
- * Build the full system prompt.
291
- *
292
- * Assembly order:
293
- * 1. Core prompt template (including the current environment block)
294
- * 2. AGENTS.md content (project-specific)
295
- * 3. Skills catalog (XML)
296
- *
297
- * @param opts - Prompt construction options.
298
- * @returns The assembled system prompt string.
299
- */
300
- export function buildSystemPrompt(opts: BuildSystemPromptOpts): string {
301
- const sections: string[] = [buildCorePrompt(opts)];
302
-
303
- // 2. AGENTS.md content
304
- if (opts.agentsMd && opts.agentsMd.length > 0) {
305
- const agentsSection = [];
306
- for (const file of opts.agentsMd) {
307
- agentsSection.push(`## ${file.path}`);
308
- agentsSection.push("");
309
- agentsSection.push(file.content);
310
- agentsSection.push("");
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.
217
+ export function insertToolUsageReminder(
218
+ messages: Message[],
219
+ 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);
311
238
  }
312
- sections.push(agentsSection.join("\n").trimEnd());
313
- }
239
+ });
240
+ const recentToolCalls = toolCalls.slice(-budget);
241
+ const taskSeen = recentToolCalls.some((call) => call.name === "task");
242
+
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.
314
246
 
315
- // 3. Skills catalog
316
- if (opts.skills && opts.skills.length > 0) {
317
- const catalog = buildSkillCatalog(opts.skills);
318
- if (catalog) sections.push(catalog);
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>
250
+
251
+ ${output}`;
319
252
  }
320
253
 
321
- return sections.join("\n\n");
254
+ toolMessage.content = [{ type: "text", text: output }];
255
+
256
+ return toolMessage;
322
257
  }