Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: add session runner log

Gus4d0fcee05ce4
diff --git a/src/cli/daemon/config.test.ts b/src/cli/daemon/config.test.ts
index 7d021caf9..36765a7a2 100644
--- a/src/cli/daemon/config.test.ts
+++ b/src/cli/daemon/config.test.ts
@@ -2,7 +2,7 @@ import { vi, describe, it, expect, afterEach } from "vitest";
import { hostname } from "os";
import { join } from "path";
import { homedir } from "os";
-import { loadDaemonConfig, normalizeServerBaseURL, daemonLogFilePath, daemonLogDir } from "./config.js";
+import { loadDaemonConfig, normalizeServerBaseURL, daemonLogFilePath, daemonLogDir, sessionRunnerLogDir } from "./config.js";
const DAEMON_ENV_KEYS = [
"ALOOK_SERVER_URL",
@@ -152,3 +152,19 @@ describe("workspacesRoot profile handling", () => {
expect(cfg.workspacesRoot).toBe("/custom/path");
});
});
+
+describe("sessionRunnerLogDir", () => {
+ it("returns <configDir>/daemon/session-runners", () => {
+ expect(sessionRunnerLogDir()).toBe(
+ join(homedir(), ".alook", "daemon", "session-runners"),
+ );
+ });
+
+ it("honours ALOOK_PROJECT_ROOT in dev mode", () => {
+ process.env.ALOOK_SERVER_URL = "http://localhost:3000";
+ process.env.ALOOK_PROJECT_ROOT = "/tmp/proj";
+ expect(sessionRunnerLogDir()).toBe(
+ join("/tmp/proj", ".alook", "daemon", "session-runners"),
+ );
+ });
+});
diff --git a/src/cli/daemon/config.ts b/src/cli/daemon/config.ts
index 7691f596e..15b904b36 100644
--- a/src/cli/daemon/config.ts
+++ b/src/cli/daemon/config.ts
@@ -11,6 +11,10 @@ export function daemonLogDir(): string {
return join(configDir(), "daemon", "logs");
}
+export function sessionRunnerLogDir(): string {
+ return join(configDir(), "daemon", "session-runners");
+}
+
export function daemonLogFilePath(date: Date = new Date()): string {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
diff --git a/src/cli/daemon/daemon.test.ts b/src/cli/daemon/daemon.test.ts
index 0a3cc3835..daf31d885 100644
--- a/src/cli/daemon/daemon.test.ts
+++ b/src/cli/daemon/daemon.test.ts
@@ -36,6 +36,7 @@ vi.mock("./config.js", () => ({
workspacesRoot: "/tmp/ws",
cliVersion: "0.1.0",
})),
+ sessionRunnerLogDir: vi.fn(() => "/tmp/alook/daemon/session-runners"),
}));
vi.mock("./health.js", () => ({
@@ -86,8 +87,23 @@ vi.mock("child_process", () => {
};
});
+const mockOpenSync = vi.fn(() => 42);
+const mockCloseSync = vi.fn();
+const mockRenameSync = vi.fn();
+const mockMkdirSync = vi.fn();
+const mockReaddirSync = vi.fn(() => [] as string[]);
+const mockStatSync = vi.fn(() => ({ mtimeMs: 0 }));
+const mockUnlinkSync = vi.fn();
+
vi.mock("fs", () => ({
existsSync: vi.fn((p: string) => p.endsWith("session-runner.js")),
+ openSync: (...args: any[]) => mockOpenSync(...args),
+ closeSync: (...args: any[]) => mockCloseSync(...args),
+ renameSync: (...args: any[]) => mockRenameSync(...args),
+ mkdirSync: (...args: any[]) => mockMkdirSync(...args),
+ readdirSync: (...args: any[]) => mockReaddirSync(...args),
+ statSync: (...args: any[]) => mockStatSync(...args),
+ unlinkSync: (...args: any[]) => mockUnlinkSync(...args),
}));
vi.mock("url", () => ({
@@ -149,7 +165,7 @@ vi.spyOn(globalThis, "clearInterval").mockImplementation(((timer: any) => {
import { spawn } from "child_process";
import { loadCLIConfigForProfile, saveCLIConfigForProfile } from "../lib/config.js";
import { releaseDaemonPid } from "./pidfile.js";
-import { startDaemon, spawnSessionRunner } from "./daemon.js";
+import { startDaemon, spawnSessionRunner, pruneSessionRunnerLogs } from "./daemon.js";
const mockReleaseDaemonPid = vi.mocked(releaseDaemonPid);
@@ -169,6 +185,7 @@ describe("daemon session runner dispatch", () => {
nextPid = 50000;
vi.clearAllMocks();
mockProcessExit.mockImplementation((() => {}) as any);
+ mockOpenSync.mockReturnValue(42);
});
afterEach(() => {
@@ -283,7 +300,7 @@ describe("daemon session runner dispatch", () => {
expect(input.model).toBe("opus");
});
- it("spawns with detached: true and calls unref()", async () => {
+ it("spawns with detached: true, log file stdio, and calls unref()", async () => {
setupTaskClaim();
await startDaemon();
@@ -291,7 +308,7 @@ describe("daemon session runner dispatch", () => {
const call = vi.mocked(spawn).mock.calls[0];
expect((call[2] as any).detached).toBe(true);
- expect((call[2] as any).stdio).toBe("ignore");
+ expect((call[2] as any).stdio).toEqual(["ignore", 42, 42]);
expect(spawnedChildren[0].unref).toHaveBeenCalled();
});
@@ -359,6 +376,7 @@ describe("daemon with multi-workspace config", () => {
nextPid = 50000;
vi.clearAllMocks();
mockProcessExit.mockImplementation((() => {}) as any);
+ mockOpenSync.mockReturnValue(42);
// Configure two workspaces
vi.mocked(loadCLIConfigForProfile).mockReturnValue({
@@ -826,10 +844,11 @@ describe("spawnSessionRunner", () => {
spawnedChildren.length = 0;
nextPid = 50000;
vi.clearAllMocks();
+ mockOpenSync.mockReturnValue(42);
});
- it("encodes input as base64 and passes to bun run", () => {
- const input = {
+ function makeSpawnInput() {
+ return {
task: { id: "t1", agentId: "a1", runtimeId: "rt1", conversationId: "c1", workspaceId: "ws1", prompt: "test", status: "dispatched", priority: 0, type: "user_dm_message", createdAt: "2026-01-01T00:00:00Z" },
provider: "claude",
cliPath: "claude",
@@ -838,9 +857,11 @@ describe("spawnSessionRunner", () => {
token: "test_token",
workspacesRoot: "/tmp/ws",
agentTimeout: 7200000,
- };
+ };
+ }
- const child = spawnSessionRunner(input as any);
+ it("encodes input as base64 and passes to bun run", () => {
+ const child = spawnSessionRunner(makeSpawnInput() as any);
expect(spawn).toHaveBeenCalledTimes(1);
const call = vi.mocked(spawn).mock.calls[0];
@@ -849,14 +870,96 @@ describe("spawnSessionRunner", () => {
const args = call[1] as string[];
expect(args[0]).toContain("session-runner.js");
- // Decode and verify
const decoded = JSON.parse(Buffer.from(args[1], "base64").toString("utf-8"));
expect(decoded.task.id).toBe("t1");
expect(decoded.provider).toBe("claude");
expect(decoded.token).toBe("test_token");
expect((call[2] as any).detached).toBe(true);
- expect((call[2] as any).stdio).toBe("ignore");
expect(child.unref).toHaveBeenCalled();
});
+
+ it("opens a log file fd and passes it as stdio [ignore, fd, fd]", () => {
+ spawnSessionRunner(makeSpawnInput() as any);
+
+ expect(mockMkdirSync).toHaveBeenCalledWith(
+ "/tmp/alook/daemon/session-runners",
+ { recursive: true },
+ );
+ expect(mockOpenSync).toHaveBeenCalledWith(
+ "/tmp/alook/daemon/session-runners/t1.log",
+ "a",
+ );
+
+ const call = vi.mocked(spawn).mock.calls[0];
+ expect((call[2] as any).stdio).toEqual(["ignore", 42, 42]);
+ });
+
+ it("closes the fd after spawn", () => {
+ spawnSessionRunner(makeSpawnInput() as any);
+ expect(mockCloseSync).toHaveBeenCalledWith(42);
+ });
+
+ it("renames the temp log file to PID.log after spawn", () => {
+ spawnSessionRunner(makeSpawnInput() as any);
+
+ expect(mockRenameSync).toHaveBeenCalledWith(
+ "/tmp/alook/daemon/session-runners/t1.log",
+ "/tmp/alook/daemon/session-runners/50000.log",
+ );
+ });
+});
+
+describe("pruneSessionRunnerLogs", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("handles missing directory gracefully", () => {
+ mockReaddirSync.mockImplementation(() => { throw new Error("ENOENT"); });
+ expect(() => pruneSessionRunnerLogs()).not.toThrow();
+ expect(mockUnlinkSync).not.toHaveBeenCalled();
+ });
+
+ it("handles empty directory gracefully", () => {
+ mockReaddirSync.mockReturnValue([]);
+ pruneSessionRunnerLogs();
+ expect(mockUnlinkSync).not.toHaveBeenCalled();
+ });
+
+ it("does not delete when at or below limit", () => {
+ const files = Array.from({ length: 50 }, (_, i) => `${i}.log`);
+ mockReaddirSync.mockReturnValue(files);
+ pruneSessionRunnerLogs();
+ expect(mockUnlinkSync).not.toHaveBeenCalled();
+ });
+
+ it("deletes oldest files when over limit, keeping newest 50", () => {
+ const files = Array.from({ length: 55 }, (_, i) => `${i}.log`);
+ mockReaddirSync.mockReturnValue(files);
+ mockStatSync.mockImplementation((p: string) => {
+ const name = p.split("/").pop()!;
+ const idx = parseInt(name);
+ return { mtimeMs: idx * 1000 };
+ });
+
+ pruneSessionRunnerLogs();
+
+ expect(mockUnlinkSync).toHaveBeenCalledTimes(5);
+ // Files 0-4 are the oldest (lowest mtime), they should be deleted
+ for (let i = 0; i < 5; i++) {
+ expect(mockUnlinkSync).toHaveBeenCalledWith(
+ `/tmp/alook/daemon/session-runners/${i}.log`,
+ );
+ }
+ });
+
+ it("ignores non-.log files", () => {
+ const files = ["1.log", "2.log", ".DS_Store", "readme.txt"];
+ mockReaddirSync.mockReturnValue(files);
+ mockStatSync.mockReturnValue({ mtimeMs: 0 });
+ pruneSessionRunnerLogs();
+ // Only 2 .log files, well under 50 limit
+ expect(mockUnlinkSync).not.toHaveBeenCalled();
+ });
});
diff --git a/src/cli/daemon/daemon.ts b/src/cli/daemon/daemon.ts
index 70d862daa..55dae2c6c 100644
--- a/src/cli/daemon/daemon.ts
+++ b/src/cli/daemon/daemon.ts
@@ -1,5 +1,5 @@
import { DaemonClient } from "./client.js";
-import { type DaemonConfig, loadDaemonConfig } from "./config.js";
+import { type DaemonConfig, loadDaemonConfig, sessionRunnerLogDir } from "./config.js";
import { createHealthServer } from "./health.js";
import { detectVersion } from "./agent/index.js";
import { type Task, type SessionRunnerInput, fromApiTask } from "./types.js";
@@ -7,7 +7,7 @@ import { loadCLIConfigForProfile, saveCLIConfigForProfile } from "../lib/config.
import { log } from "../lib/logger.js";
import { cmdPrefix } from "../lib/env.js";
import { acquireDaemonPid, releaseDaemonPid } from "./pidfile.js";
-import { existsSync } from "fs";
+import { existsSync, mkdirSync, openSync, closeSync, renameSync, readdirSync, statSync, unlinkSync } from "fs";
import { execSync, spawn, type ChildProcess } from "child_process";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
@@ -38,10 +38,43 @@ function isCommandAvailable(cmd: string): boolean {
}
}
+const MAX_SESSION_RUNNER_LOGS = 50;
+
+export function pruneSessionRunnerLogs(): void {
+ const logDir = sessionRunnerLogDir();
+ let entries: string[];
+ try {
+ entries = readdirSync(logDir).filter((f) => f.endsWith(".log"));
+ } catch {
+ return;
+ }
+ if (entries.length <= MAX_SESSION_RUNNER_LOGS) return;
+
+ const withMtime = entries.map((name) => {
+ const full = join(logDir, name);
+ try {
+ return { name, mtime: statSync(full).mtimeMs };
+ } catch {
+ return { name, mtime: 0 };
+ }
+ });
+ withMtime.sort((a, b) => b.mtime - a.mtime);
+
+ for (const entry of withMtime.slice(MAX_SESSION_RUNNER_LOGS)) {
+ try {
+ unlinkSync(join(logDir, entry.name));
+ } catch {
+ // best-effort
+ }
+ }
+}
+
export async function startDaemon(
profile?: string,
serverUrl?: string,
): Promise<void> {
+ pruneSessionRunnerLogs();
+
if (!acquireDaemonPid(profile)) {
process.exit(1);
}
@@ -251,11 +284,24 @@ export async function startDaemon(
export function spawnSessionRunner(input: SessionRunnerInput): ChildProcess {
const encoded = Buffer.from(JSON.stringify(input)).toString("base64");
+
+ const logDir = sessionRunnerLogDir();
+ mkdirSync(logDir, { recursive: true });
+ const tmpLogPath = join(logDir, `${input.task.id}.log`);
+ const fd = openSync(tmpLogPath, "a");
+
const child = spawn(process.execPath, [sessionRunnerPath, encoded], {
detached: true,
- stdio: "ignore",
+ stdio: ["ignore", fd, fd],
});
child.unref();
+ closeSync(fd);
+
+ if (child.pid) {
+ const pidLogPath = join(logDir, `${child.pid}.log`);
+ renameSync(tmpLogPath, pidLogPath);
+ }
+
return child;
}