xonecas.io
64875
Mc

mini-coder / commit f9dfa65

perf: cache system prompt and batch message writes

xonecas2026-03-14 f9dfa65af1f73561153835be7dd8bd348932b5d0 parent 4df3be8

4 files changed

TODO.md+1−12
@@ -9,14 +9,6 @@
99
1010 ---
1111
12-## Lean and mean idea
13-
14-When mc is called with a prompt, make it exit after the loop. Like a one shot. ✅
15-Drop glob and grep tools — done. ✅
16-Cleanup pass — done. ✅
17-
18----
19-
2012 ## UI Audit
2113
2214 - We need to revise all of our output to ensure consistency, **performance** and correctness.
@@ -24,10 +16,6 @@ Cleanup pass — done. ✅
2416 - Ensure we have propper hierchy in output, and the different types of output are clearly distinguishable for the user, using styles and whitespace.
2517 - Ensure proper spinner functionality, that follow up messages don't rended inline and that is doesn't break anything.
2618
27-## CLI vs headless mode
28-
29-- I need to understand this better tell me all about what is headless mode and what it does in our code.
30-
3119 ---
3220
3321 ## LSP Diagnostics (not very important, with tool-hooks and strong linting, this is not as necessary, but we should implement asap)
@@ -44,3 +32,4 @@ We need to do research first, but maybe we can leverage the hooks feature to ach
4432
4533 - model-info: in `resolveFromProviderRow`, when canonical capability exists but `contextWindow` is null, fall back to provider row context (`capability.contextWindow ?? row.contextWindow`).
4634 - subagent-runner: avoid unconditional full buffering of child `stdout`/`stderr` in `runSubagent`; capture diagnostics only on failure or via a bounded tail buffer to prevent latency/memory regressions.
35+- Subagent and shell tools are very similar, shell could do what subagent does without changes. This could be leveraged to reduce code. Subagent process runner is used for /ralph and custom commands that fork context as well, there will need to be refactored.
mini-coder-idea.md+8−8
@@ -1,6 +1,6 @@
11 # mini-coder
22
3-A coding agent via a augmented CLI, small and fast that doesn't get in the way
3+A coding agent via a augmented shell prompt, small and fast that doesn't get in the way
44
55 ## Inpirations:
66
@@ -25,8 +25,8 @@ conventions and not introduce more specs. (`.agents` / `AGENTS.md`, while also s
2525 - Seamless shell integration with `!` in prompt input.
2626 - Reference files and skills from the working directory and global configs with `@` in prompt input, plus autocomplete for files, skills, and agents.
2727 - Press `ESC` at any point during an assistant response to interrupt it: the partial response is preserved in history with an interrupt stub appended (so the LLM retains context), and the user is returned to the prompt silently. `ctrl+c` exits the app. `ctrl+d` (EOF) also exits.
28-
29-- `glob`, `grep`, `read`, `replace`,`insert`, `create`, `shell`, and `subagent` tools for LLMs
28+- Feature parity with community configs like custom agents, skill and commands.
29+- `read`, `replace`,`insert`, `create`, `shell`, and `subagent` tools for LLMs
3030 - Optional `webSearch` and `webContent` tools when `EXA_API_KEY` is set.
3131 - `subagent` tool spawns a fresh `mc` subprocess — full capability parity with the main agent, support for recursive subagents (up to 10 levels). Custom agents and custom commands from `.agents` and `.claude` are supported in subagents.
3232 - tool hooks support for supported built-in tools
@@ -36,7 +36,6 @@ conventions and not introduce more specs. (`.agents` / `AGENTS.md`, while also s
3636 - `/reasoning` toggles display of model reasoning output.
3737 - `/context` configures context pruning and tool-result caps.
3838 - `/review` reviews recent changes via a global command installed at app start (`~/.agents/commands/review.md`), and can be customized or shadowed locally.
39-
4039 - `/plan` for a read-only mode (`/plan` again to turn off)
4140 - `/ralph` for a ralph loop mode (by Geoffrey Huntley, who described it simply: “Ralph is a Bash loop.”)
4241 - `/agent` sets or clears the active primary agent.
@@ -44,17 +43,18 @@ conventions and not introduce more specs. (`.agents` / `AGENTS.md`, while also s
4443 - `/new` starts a new session with clean context.
4544 - Connect to MCP servers over Streamable HTTP / SSE fallback or stdio.
4645 - Image support in prompt input, including pasted image data URLs and pasted image file paths.
46+- Excellent auto complete for all commands, subtituitions. The user should also be able to reference files with autocomplete wihtout including them into the prompt like `@` does.
4747
4848 ## UI/Output
4949
5050 - Use the 16 ANSII colors so the coloring is allways inherited from the terminal theme (https://jvns.ca/blog/2024/10/01/terminal-colours/)
51-- Use oh-my-pi and claude code as visual inspirations.
51+- Use pi and claude code as visual inspirations.
5252 - history log (like a terminal output scrolls away, pushing the input prompt down)
53- - Read tools (glob, grep, read) show the tool call with args fomatted for output
53+ - Read tool show the tool call with args fomatted for output
5454 - Edit tools show the diff applied
5555 - Shell tools show the command called and their output
5656 - status bar under prompt, with current model/provider/session/git branch/active agent/thinking effort/context usage/token input/token output
57-- some prompt animation when a turn is processing.
57+- some prompt colored animation when a turn is processing.
5858
5959 ## Tech stack
6060
@@ -74,4 +74,4 @@ Core modules:
7474 - `agent`: Main agent implementation
7575 - `tools`: local filesystem, shell, subagent, and web/search helpers
7676 - `session`: sqlite-backed sessions, settings, model info, and MCP server storage
77-- `mcp`: handles connecting to mcp servers
77+- `mcp`: handles connecting to mcp servers
src/agent/session-runner.ts+20−13
@@ -84,16 +84,29 @@ export class SessionRunner {
8484 public lastContextTokens = 0;
8585 // Cache the system prompt so the file-system reads in buildSystemPrompt
8686 // (context files, skills index) are not repeated on every processUserInput call.
87- // Re-built only when extraSystemPrompt changes.
8887 private _extraSystemPrompt: string | undefined;
8988 private _systemPrompt: string | undefined;
9089
9190 public get extraSystemPrompt(): string | undefined {
9291 return this._extraSystemPrompt;
9392 }
93+
9494 public set extraSystemPrompt(value: string | undefined) {
9595 this._extraSystemPrompt = value;
96- this._systemPrompt = undefined; // invalidate cache
96+ this.rebuildSystemPrompt();
97+ }
98+
99+ private rebuildSystemPrompt(): void {
100+ if (!this.sessionTimeAnchor) {
101+ this._systemPrompt = undefined;
102+ return;
103+ }
104+ this._systemPrompt = buildSystemPrompt(
105+ this.sessionTimeAnchor,
106+ this.cwd,
107+ this._extraSystemPrompt,
108+ this.isSubagent,
109+ );
97110 }
98111
99112 private isSubagent: boolean | undefined;
@@ -112,9 +125,9 @@ export class SessionRunner {
112125 this.promptCachingEnabled = opts.initialPromptCachingEnabled;
113126 this.openaiPromptCacheRetention = opts.initialOpenAIPromptCacheRetention;
114127 this.googleCachedContent = opts.initialGoogleCachedContent;
115- this.extraSystemPrompt = opts.extraSystemPrompt;
116128 this.isSubagent = opts.isSubagent;
117129 this.killSubprocesses = opts.killSubprocesses;
130+ this.extraSystemPrompt = opts.extraSystemPrompt;
118131 this.initSession(opts.sessionId);
119132 }
120133
@@ -142,6 +155,7 @@ export class SessionRunner {
142155
143156 this.sessionTimeAnchor = new Date(this.session.createdAt).toISOString();
144157 this.coreHistory = [...this.session.messages];
158+ this.rebuildSystemPrompt();
145159 }
146160
147161 public startNewSession() {
@@ -153,7 +167,7 @@ export class SessionRunner {
153167 this.totalOut = 0;
154168 this.lastContextTokens = 0;
155169 this.sessionTimeAnchor = new Date(this.session.createdAt).toISOString();
156- this._systemPrompt = undefined; // invalidate cached prompt (new session anchor)
170+ this.rebuildSystemPrompt();
157171 this.snapshotStack.length = 0;
158172 }
159173
@@ -199,15 +213,8 @@ export class SessionRunner {
199213 this.coreHistory.push(userMsg);
200214
201215 const llm = resolveModel(this.currentModel);
202- // Use cached system prompt; rebuild only on first use or after
203- // extraSystemPrompt / sessionTimeAnchor change.
204216 if (!this._systemPrompt) {
205- this._systemPrompt = buildSystemPrompt(
206- this.sessionTimeAnchor,
207- this.cwd,
208- this._extraSystemPrompt,
209- this.isSubagent,
210- );
217+ this.rebuildSystemPrompt();
211218 }
212219 const systemPrompt = this._systemPrompt;
213220
@@ -223,7 +230,7 @@ export class SessionRunner {
223230 tools: this.planMode
224231 ? [...buildReadOnlyToolSet({ cwd: this.cwd }), ...this.mcpTools]
225232 : this.tools,
226- systemPrompt,
233+ ...(systemPrompt ? { systemPrompt } : {}),
227234 signal: abortController.signal,
228235 pruningMode: this.pruningMode,
229236 toolResultPayloadCapBytes: this.toolResultPayloadCapBytes,
src/session/db/message-repo.ts+26−11
@@ -2,9 +2,12 @@ import { renderError } from "../../cli/output.ts";
22 import type { CoreMessage } from "../../llm-api/turn.ts";
33 import { getDb } from "./connection.ts";
44
5-// Hoist prepared statement to module-level lazy singleton to avoid re-compiling SQL each call.
5+// Hoist prepared statements to module-level lazy singletons so hot-path calls
6+// don't re-compile SQL every turn.
67 type Stmt = ReturnType<ReturnType<typeof getDb>["prepare"]>;
78 let _insertMsgStmt: Stmt | null = null;
9+let _addPromptHistoryStmt: Stmt | null = null;
10+let _getPromptHistoryStmt: Stmt | null = null;
811 function getInsertMsgStmt(): Stmt {
912 if (!_insertMsgStmt) {
1013 _insertMsgStmt = getDb().prepare(
@@ -15,6 +18,24 @@ function getInsertMsgStmt(): Stmt {
1518 return _insertMsgStmt;
1619 }
1720
21+function getAddPromptHistoryStmt(): Stmt {
22+ if (!_addPromptHistoryStmt) {
23+ _addPromptHistoryStmt = getDb().prepare(
24+ "INSERT INTO prompt_history (text, created_at) VALUES (?, ?)",
25+ );
26+ }
27+ return _addPromptHistoryStmt;
28+}
29+
30+function getPromptHistoryStmt(): Stmt {
31+ if (!_getPromptHistoryStmt) {
32+ _getPromptHistoryStmt = getDb().prepare(
33+ "SELECT text FROM prompt_history ORDER BY id DESC LIMIT ?",
34+ );
35+ }
36+ return _getPromptHistoryStmt;
37+}
38+
1839 export function saveMessages(
1940 sessionId: string,
2041 msgs: CoreMessage[],
@@ -93,18 +114,12 @@ export function loadMessages(sessionId: string): CoreMessage[] {
93114 }
94115
95116 export function addPromptHistory(text: string): void {
96- if (!text.trim()) return;
97- getDb().run("INSERT INTO prompt_history (text, created_at) VALUES (?, ?)", [
98- text.trim(),
99- Date.now(),
100- ]);
117+ const trimmed = text.trim();
118+ if (!trimmed) return;
119+ getAddPromptHistoryStmt().run(trimmed, Date.now());
101120 }
102121
103122 export function getPromptHistory(limit = 200): string[] {
104- const rows = getDb()
105- .query<{ text: string }, [number]>(
106- "SELECT text FROM prompt_history ORDER BY id DESC LIMIT ?",
107- )
108- .all(limit);
123+ const rows = getPromptHistoryStmt().all(limit) as Array<{ text: string }>;
109124 return rows.map((r) => r.text).reverse();
110125 }