xonecas.io
64875
Mc

mini-coder / commit 7192c76

feat: session persistence

xonecas2026-04-28 7192c76c87b211bc6f599c54384f0adeb444c52e parent 9a6200b

6 files changed

AGENTS.md+1−1
@@ -1,6 +1,6 @@
11 # Mini coder next
22
3-A new version of mini-coder, written from the ground up as library first.
3+A new version of mini-coder, written from the ground up.
44 Version `mini-coder@0.5.x` is currently stable and mostly complete, that's our reference implementation.
55
66 ## Dog-fooding
src/prompt.ts+8−4
@@ -17,13 +17,15 @@ const safetyPrompt = `
1717 - Do not over-scope your work, or add more scope during implementation.
1818 - Avoid over-enginnering, hacks or creative solutions. The boring, simple and repliable is always preffered.
1919 - Do not overstate what changed or what was verified. Summaries must match the diff.
20-`
20+`;
2121
2222 export const MAIN_PROMPT = `# You are "mini-coder", an efficient and elite level coding agent.
2323
2424 ## Behaviour:
2525
2626 - Be efficient, don't get lost with tangents or satisfying your curiosity, root yourself on the user request.
27+- Always prefer the task tool. It's the intended way of working.
28+- Keep other tools for single call actions, anything more, you should use the task tool.
2729 - Narrate your edits with small commentary messages during long tasks.
2830 - Focus on the user's request requirements to answer accurately and efficiently.
2931 - Once you've gathered enough information to complete the request, stop exploring and complete it.
@@ -201,10 +203,12 @@ export function insertToolUsageReminder(
201203 .map((b) => b.text)
202204 .join("\n");
203205
204- const budget = 5;
206+ const budget = 3;
205207 const toolCalls: ToolCall[] = [];
206- // TODO: Change this so it's only the messages since the last user message.
207- messages.forEach((m) => {
208+ const lastUserMessageIndex = messages.findLastIndex((m) => m.role === "user");
209+ const messagesSinceLastUser = messages.slice(lastUserMessageIndex + 1);
210+
211+ messagesSinceLastUser.forEach((m) => {
208212 if (m.role === "assistant") {
209213 const toolCallsBlocks = m.content.filter((b) => b.type === "toolCall");
210214 toolCalls.push(...toolCallsBlocks);
src/session.ts+55−0
@@ -0,0 +1,55 @@
1+import { mkdir } from "node:fs/promises";
2+import { join } from "node:path";
3+import { Value } from "typebox/value";
4+import { SESSIONS_DIR } from "./shared";
5+import { Session, SessionSchema } from "./types";
6+import { Message } from "@mariozechner/pi-ai";
7+
8+// TODO: sessions are json files in SESSIONS_DIR inside of DATA_DIR, use a 10 length `secureRandomString()` for the ids.
9+
10+export async function ensureSessionsDir(): Promise<void> {
11+ await mkdir(SESSIONS_DIR, { recursive: true });
12+}
13+
14+// readSession: writes the session
15+export async function getSession(id: string): Promise<Session | undefined> {
16+ const file = Bun.file(join(SESSIONS_DIR, `${id}.json`));
17+
18+ if (!(await file.exists())) {
19+ return;
20+ }
21+
22+ const sessionJson = await file.text();
23+ const parsed = JSON.parse(sessionJson) as unknown
24+ const valid = Value.Check(
25+ SessionSchema,
26+ parsed
27+ );
28+
29+ if (valid) return parsed;
30+ return;
31+}
32+
33+export async function saveSession(s: Session) {
34+ await ensureSessionsDir()
35+ const file = Bun.file(join(SESSIONS_DIR, `${s.id}.json`))
36+ await Bun.write(file, JSON.stringify(s));
37+}
38+
39+// updateSession: finds and appends to the existing setting file
40+export async function updateSession(id: string, messages: Message[]) {
41+ const existing = await getSession(id);
42+ if (existing) {
43+ existing.messages = [...existing.messages, ...messages]
44+ await saveSession(existing)
45+ return;
46+ }
47+
48+ const s = {
49+ id,
50+ cwd: process.cwd(),
51+ messages,
52+ }
53+
54+ await saveSession(s);
55+}
src/shared.ts+1−0
@@ -5,6 +5,7 @@ import { parseDocument } from "yaml";
55 // Mixed bag of helpers that can be shared across the codebase
66
77 export const DATA_DIR = join(homedir(), ".config", "mini-coder");
8+export const SESSIONS_DIR = join(DATA_DIR, "sessions");
89 export const AUTH_PATH = join(DATA_DIR, "auth.json");
910 export const SETTINGS_PATH = join(DATA_DIR, "settings.json");
1011
src/tui.ts+9−1
@@ -6,7 +6,7 @@ import {
66 insertToolUsageReminder,
77 MAIN_PROMPT,
88 } from "./prompt";
9-import { estimateTokens } from "./shared";
9+import { estimateTokens, secureRandomString } from "./shared";
1010 import { bash, runBashTool } from "./tool-bash";
1111 import { edit, runEditTool } from "./tool-edit";
1212 import { runTaskTool, task } from "./tool-task";
@@ -23,6 +23,7 @@ import { Conversation, emptyState } from "./tui-conversation";
2323 import { Editor } from "./tui-editor";
2424 import { mainMenu } from "./tui-overlay";
2525 import type { AgentContex, ToolAndRunner, TUIState } from "./types";
26+import { updateSession } from "./session";
2627
2728 // TODO: move all git things to `git.ts`
2829 const git = simpleGit();
@@ -40,6 +41,8 @@ function clearOrAbort(state: TUIState) {
4041 }
4142
4243 export function initTUI(state: TUIState, leave: (s: string) => void) {
44+ // TODO: Cleanup accumulated sessions for this cwd.
45+
4346 const { spinnerEvery, currentSpinner } = Spinner();
4447
4548 // Stable 60fps rendering.
@@ -195,6 +198,11 @@ async function streamAgentTUI(state: TUIState) {
195198 }
196199 } finally {
197200 state.streaming = false;
201+ if (!state.sessionId) {
202+ const id = secureRandomString(10);
203+ state.sessionId = id;
204+ }
205+ await updateSession(state.sessionId, state.messages)
198206 }
199207
200208 try {
src/types.ts+10−0
@@ -71,6 +71,15 @@ type SavedOAuthAuth = OAuthCredentials & {
7171 };
7272 export type SavedOAuthCreds = Record<string, SavedOAuthAuth>;
7373
74+export const MessageSchema = Type.Unsafe<Message>;
75+export const SessionSchema = Type.Object({
76+ id: Type.String(),
77+ cwd: Type.String(),
78+ messages: Type.Array(MessageSchema),
79+});
80+export type Session = Static<typeof SessionSchema>;
81+export type Sessions = Session[];
82+
7483 export type TUIState = {
7584 options: CliOptions;
7685 prompt: string;
@@ -83,6 +92,7 @@ export type TUIState = {
8392 cwd: string;
8493 gitBranch?: string;
8594 overlay?: boolean | undefined;
95+ sessionId: string;
8696 };
8797
8898 export type AgentContex = {