xonecas.io
64875
Mc

mini-coder / commit b96b770

fix: use PID-scoped log files for safe concurrent access

- Each process writes to its own api-<pid>.log and errors-<pid>.log
- Remove subagent guards from initApiLog/initErrorLog since PID-scoping
  eliminates write conflicts
- Add cleanStaleLogs() to remove log files from dead processes on startup
- Only run stale log cleanup from top-level processes (not subagents)
  to avoid races and unnecessary overhead

xonecas2026-03-17 b96b770e1a36688ff3a1334ab59531f510b5e746 parent 913e461

4 files changed

src/cli/error-log.ts+5−12
@@ -1,22 +1,15 @@
1-import { mkdirSync, writeFileSync } from "node:fs";
2-import { homedir } from "node:os";
3-import { join } from "node:path";
1+import { writeFileSync } from "node:fs";
2+
3+import { pidLogPath } from "./log-paths.ts";
44
55 let writer: ReturnType<ReturnType<typeof Bun.file>["writer"]> | null = null;
66
77 export function initErrorLog(): void {
88 if (writer) return;
9- // Subagent processes must not truncate/write the shared log file (same
10- // sparse-hole issue as api-log.ts — see comment there for details).
11- if (process.env.MC_SUBAGENT_DEPTH && process.env.MC_SUBAGENT_DEPTH !== "0")
12- return;
13-
14- const dirPath = join(homedir(), ".config", "mini-coder");
15- const logPath = join(dirPath, "errors.log");
169
17- mkdirSync(dirPath, { recursive: true });
10+ const logPath = pidLogPath("errors");
1811
19- // Truncate on open so it doesn't grow forever between sessions
12+ // Truncate on open so each session starts fresh
2013 writeFileSync(logPath, "");
2114 writer = Bun.file(logPath).writer();
2215
src/cli/log-paths.ts+55−0
@@ -0,0 +1,55 @@
1+import { mkdirSync, readdirSync, unlinkSync } from "node:fs";
2+import { homedir } from "node:os";
3+import { join } from "node:path";
4+
5+const LOGS_DIR = join(homedir(), ".config", "mini-coder", "logs");
6+
7+/** Return the logs directory, creating it if necessary. */
8+function getLogsDir(): string {
9+ mkdirSync(LOGS_DIR, { recursive: true });
10+ return LOGS_DIR;
11+}
12+
13+/** Build a PID-scoped log file path: `<logsDir>/<prefix>-<pid>.log` */
14+export function pidLogPath(prefix: string): string {
15+ return join(getLogsDir(), `${prefix}-${process.pid}.log`);
16+}
17+
18+function isProcessAlive(pid: number): boolean {
19+ try {
20+ process.kill(pid, 0);
21+ return true;
22+ } catch {
23+ return false;
24+ }
25+}
26+
27+const LOG_FILE_RE = /^(api|errors)-(\d+)\.log$/;
28+
29+/**
30+ * Remove log files left by dead processes. Called once on startup by
31+ * non-subagent processes so the logs directory doesn't grow unbounded.
32+ */
33+export function cleanStaleLogs(): void {
34+ let entries: string[];
35+ try {
36+ entries = readdirSync(LOGS_DIR);
37+ } catch {
38+ return;
39+ }
40+
41+ for (const name of entries) {
42+ const m = LOG_FILE_RE.exec(name);
43+ if (!m) continue;
44+ const pidStr = m[2] as string;
45+ const pid = Number.parseInt(pidStr, 10);
46+ if (pid === process.pid) continue;
47+ if (!isProcessAlive(pid)) {
48+ try {
49+ unlinkSync(join(LOGS_DIR, name));
50+ } catch {
51+ /* race: another process may have already cleaned it */
52+ }
53+ }
54+ }
55+}
src/index.ts+6−0
@@ -10,6 +10,7 @@ import { initErrorLog } from "./cli/error-log.ts";
1010 import { resolveFileRefs } from "./cli/file-refs.ts";
1111 import { HeadlessReporter } from "./cli/headless-reporter.ts";
1212 import { runInputLoop } from "./cli/input-loop.ts";
13+import { cleanStaleLogs } from "./cli/log-paths.ts";
1314 import {
1415 CliReporter,
1516 G,
@@ -49,6 +50,11 @@ import { getMostRecentSession, printSessionList } from "./session/manager.ts";
4950 registerTerminalCleanup();
5051 initErrorLog();
5152 initApiLog();
53+// Only clean stale logs from the top-level process to avoid races
54+// between sibling subagents and unnecessary overhead in short-lived subprocesses.
55+if (!process.env.MC_SUBAGENT_DEPTH || process.env.MC_SUBAGENT_DEPTH === "0") {
56+ cleanStaleLogs();
57+}
5258 initModelInfoCache();
5359 pruneOldData();
5460 void refreshModelInfoInBackground().catch(() => {});
src/llm-api/api-log.ts+5−13
@@ -1,6 +1,6 @@
1-import { mkdirSync, writeFileSync } from "node:fs";
2-import { homedir } from "node:os";
3-import { join } from "node:path";
1+import { writeFileSync } from "node:fs";
2+
3+import { pidLogPath } from "../cli/log-paths.ts";
44
55 let writer: ReturnType<ReturnType<typeof Bun.file>["writer"]> | null = null;
66
@@ -10,18 +10,10 @@ const MAX_ENTRY_BYTES = 8 * 1024;
1010
1111 export function initApiLog(): void {
1212 if (writer) return;
13- // Subagent processes must not truncate/write the shared log file — the parent
14- // process already owns the writer and a second truncation creates a sparse
15- // null-byte hole when the parent resumes writing at its old file offset.
16- if (process.env.MC_SUBAGENT_DEPTH && process.env.MC_SUBAGENT_DEPTH !== "0")
17- return;
18-
19- const dirPath = join(homedir(), ".config", "mini-coder");
20- const logPath = join(dirPath, "api.log");
2113
22- mkdirSync(dirPath, { recursive: true });
14+ const logPath = pidLogPath("api");
2315
24- // Truncate on open so it doesn't grow forever between sessions
16+ // Truncate on open so each session starts fresh
2517 writeFileSync(logPath, "");
2618 writer = Bun.file(logPath).writer();
2719 }