xonecas.io
64875
Mc

mini-coder / commit 09d5af4

feat: prefer shell mc-edit workflow

xonecas2026-03-15 09d5af43126e9db50042e1efbb4e934644980940 parent d1f6c39

8 files changed

src/agent/subagent-runner.ts+3−7
@@ -1,3 +1,4 @@
1+import { resolveProcessScriptPath } from "../internal/runtime/script.ts";
12 import type { SubagentOutput, SubagentSummary } from "../tools/subagent.ts";
23
34 /**
@@ -10,13 +11,8 @@ export function resolveMcCommand(
1011 mainModule: string | undefined,
1112 argv1: string | undefined,
1213 ): string[] {
13- const script =
14- mainModule &&
15- !mainModule.endsWith("/[eval]") &&
16- !mainModule.endsWith("\\[eval]")
17- ? mainModule
18- : argv1;
19- if (script && /\.(?:[cm]?[jt]s)$/.test(script)) {
14+ const script = resolveProcessScriptPath(mainModule, argv1);
15+ if (script) {
2016 return [execPath, script];
2117 }
2218 return [execPath];
src/agent/system-prompt.test.ts+7−0
@@ -58,6 +58,13 @@ describe("buildSystemPrompt", () => {
5858 expect(prompt).toContain("# Workspace guardrails");
5959 expect(prompt).toContain("# Progress communication");
6060 expect(prompt).toContain("# Final response style");
61+ expect(prompt).toContain(
62+ "Prefer shell for reading, searching, verification",
63+ );
64+ expect(prompt).toContain("prefer shell commands that invoke `mc-edit`");
65+ expect(prompt).not.toContain(
66+ "Prefer dedicated tools (read, replace, insert) over shell for file operations.",
67+ );
6168 expect(prompt).toContain("Do not guess unknown facts");
6269 });
6370
src/agent/system-prompt.ts+10−4
@@ -86,7 +86,7 @@ export function buildSystemPrompt(
8686 const localContext = loadLocalContextFile(cwd);
8787 const cwdDisplay = tildePath(cwd);
8888 let prompt = `You are mini-coder, a small and fast CLI coding agent.
89-You have access to tools to read files, search code, make edits, run shell commands, and spawn subagents.
89+You have access to tools to read files, search code, create files, run shell commands, and spawn subagents.
9090
9191 Current working directory: ${cwdDisplay}
9292 Current date/time: ${sessionTimeAnchor}
@@ -94,12 +94,18 @@ Current date/time: ${sessionTimeAnchor}
9494 Guidelines:
9595 - Be concise and precise. Avoid unnecessary preamble.
9696 - Prefer small, targeted edits over large rewrites.
97-- Always read a file before editing it. Prefer dedicated tools (read, replace, insert) over shell for file operations.
97+- Inspect files before editing them.
98+- Prefer shell for reading, searching, verification, and other general repo work.
99+- For partial edits to existing files, prefer shell commands that invoke \`mc-edit\`.
100+- Use \`create\` for new files or full-file rewrites.
98101 - Make parallel tool calls when reads/searches are independent — don't wait for one to start the next.
99102 - Keep your context clean and focused on the user request.
100103
101-# Tool output format
102-\`read\` prefixes every line with \`line:hash|\` (e.g. \`11:a3| code here\`). This prefix is metadata — never include it in file content you write. Use the \`line:hash\` values as anchors for \`replace\` and \`insert\`.`;
104+# Preferred file workflow
105+- \`mc-edit\` is available inside shell commands.
106+- \`mc-edit\` applies one exact-text edit and fails if the expected old text is missing or ambiguous.
107+- For longer edits, write the old/new text to temp files and pass \`--old-file\` / \`--new-file\`.
108+- If you use \`read\`, its \`line:hash|\` prefixes are metadata only — never copy them into file content.`;
103109
104110 prompt += AUTONOMY;
105111 prompt += SAFETY;
src/internal/file-edit/command.test.ts+48−0
@@ -0,0 +1,48 @@
1+import { describe, expect, test } from "bun:test";
2+import {
3+ buildFileEditShellPrelude,
4+ resolveFileEditCommand,
5+} from "./command.ts";
6+
7+describe("resolveFileEditCommand", () => {
8+ test("resolves the source helper beside src/index.ts", () => {
9+ expect(
10+ resolveFileEditCommand(
11+ "/usr/bin/bun",
12+ "/repo/src/index.ts",
13+ undefined,
14+ import.meta.url,
15+ ),
16+ ).toEqual(["/usr/bin/bun", "/repo/src/mc-edit.ts"]);
17+ });
18+
19+ test("resolves the built helper beside dist/mc.js", () => {
20+ expect(
21+ resolveFileEditCommand(
22+ "/usr/bin/bun",
23+ "/repo/dist/mc.js",
24+ undefined,
25+ import.meta.url,
26+ ),
27+ ).toEqual(["/usr/bin/bun", "/repo/dist/mc-edit.js"]);
28+ });
29+
30+ test("falls back to mc-edit when no script entrypoint is available", () => {
31+ expect(
32+ resolveFileEditCommand(
33+ "/usr/bin/bun",
34+ "/repo/[eval]",
35+ undefined,
36+ "file:///nonexistent/command.ts",
37+ ),
38+ ).toEqual(["mc-edit"]);
39+ });
40+});
41+
42+describe("buildFileEditShellPrelude", () => {
43+ test("defines an mc-edit shell function that preserves arguments", () => {
44+ expect(buildFileEditShellPrelude(["bun", "/repo/src/mc-edit.ts"])).toBe(
45+ "mc-edit() { 'bun' '/repo/src/mc-edit.ts' \"$@\"; }",
46+ );
47+ });
48+});
src/internal/file-edit/command.ts+62−0
@@ -0,0 +1,62 @@
1+import { existsSync } from "node:fs";
2+import { dirname, extname, join } from "node:path";
3+import { fileURLToPath } from "node:url";
4+import { resolveProcessScriptPath } from "../runtime/script.ts";
5+
6+function quoteShellArg(value: string): string {
7+ return `'${value.replaceAll("'", `'\\''`)}'`;
8+}
9+
10+function resolveSiblingFileEditScript(scriptPath: string): string | null {
11+ const ext = extname(scriptPath);
12+ if (!ext) return null;
13+
14+ const mainDir = dirname(scriptPath);
15+ const mainBase = scriptPath.slice(mainDir.length + 1);
16+ if (mainBase === `index${ext}` || mainBase === `mc${ext}`) {
17+ return join(mainDir, `mc-edit${ext}`);
18+ }
19+
20+ return null;
21+}
22+
23+function resolveModuleLocalFileEditScript(moduleUrl: string): string | null {
24+ const modulePath = fileURLToPath(moduleUrl);
25+ const ext = extname(modulePath);
26+ if (!ext) return null;
27+
28+ const helperPath = join(dirname(modulePath), "..", "..", `mc-edit${ext}`);
29+ return existsSync(helperPath) ? helperPath : null;
30+}
31+
32+export function resolveFileEditCommand(
33+ execPath: string,
34+ mainModule: string | undefined,
35+ argv1: string | undefined,
36+ moduleUrl = import.meta.url,
37+): string[] {
38+ const scriptPath = resolveProcessScriptPath(mainModule, argv1);
39+ const helperScript = scriptPath
40+ ? resolveSiblingFileEditScript(scriptPath)
41+ : null;
42+ if (helperScript) {
43+ return [execPath, helperScript];
44+ }
45+
46+ const moduleLocalHelper = resolveModuleLocalFileEditScript(moduleUrl);
47+ if (moduleLocalHelper) {
48+ return [execPath, moduleLocalHelper];
49+ }
50+
51+ return ["mc-edit"];
52+}
53+
54+function getFileEditCommand(): string[] {
55+ return resolveFileEditCommand(process.execPath, Bun.main, process.argv[1]);
56+}
57+
58+export function buildFileEditShellPrelude(
59+ command = getFileEditCommand(),
60+): string {
61+ return `mc-edit() { ${command.map(quoteShellArg).join(" ")} "$@"; }`;
62+}
src/internal/runtime/script.ts+13−0
@@ -0,0 +1,13 @@
1+export function resolveProcessScriptPath(
2+ mainModule: string | undefined,
3+ argv1: string | undefined,
4+): string | null {
5+ const script =
6+ mainModule &&
7+ !mainModule.endsWith("/[eval]") &&
8+ !mainModule.endsWith("\\[eval]")
9+ ? mainModule
10+ : argv1;
11+
12+ return script && /\.(?:[cm]?[jt]s)$/.test(script) ? script : null;
13+}
src/tools/shell.test.ts+10−0
@@ -27,6 +27,16 @@ describe("shellTool", () => {
2727 await expectStreamedOutput("printf 'ok\\n'", "ok\n");
2828 });
2929
30+ it("injects mc-edit into shell commands", async () => {
31+ const result = await shellTool.execute({
32+ command: "mc-edit --help >/dev/null",
33+ timeout: 30_000,
34+ });
35+
36+ expect(result.success).toBe(true);
37+ expect(result.exitCode).toBe(0);
38+ });
39+
3040 it("marks output as not streamed when no onOutput handler is provided", async () => {
3141 const result = await shellTool.execute({
3242 command: "printf ok",
src/tools/shell.ts+10−6
@@ -1,5 +1,6 @@
11 import { z } from "zod";
22 import { restoreTerminal } from "../cli/output.ts";
3+import { buildFileEditShellPrelude } from "../internal/file-edit/command.ts";
34 import type { ToolDef } from "../llm-api/types.ts";
45
56 const ShellSchema = z.object({
@@ -55,12 +56,15 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
5556
5657 const wasRaw = process.stdin.isTTY ? process.stdin.isRaw : false;
5758
58- const proc = Bun.spawn(["bash", "-c", input.command], {
59- cwd,
60- env,
61- stdout: "pipe",
62- stderr: "pipe",
63- });
59+ const proc = Bun.spawn(
60+ ["bash", "-c", `${buildFileEditShellPrelude()}\n${input.command}`],
61+ {
62+ cwd,
63+ env,
64+ stdout: "pipe",
65+ stderr: "pipe",
66+ },
67+ );
6468
6569 // Set up timeout
6670 const timer = setTimeout(() => {