From 32e2c369d754e5e31a42a232940867366d25997e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 16 Feb 2026 23:47:20 +0000 Subject: [PATCH] refactor(agents): extract shared session dir resolver --- src/agents/session-dirs.ts | 22 ++++++++++++++++++++++ src/commands/doctor-session-locks.ts | 23 +---------------------- src/gateway/server-startup.ts | 23 +---------------------- 3 files changed, 24 insertions(+), 44 deletions(-) create mode 100644 src/agents/session-dirs.ts diff --git a/src/agents/session-dirs.ts b/src/agents/session-dirs.ts new file mode 100644 index 0000000000..1985dcf608 --- /dev/null +++ b/src/agents/session-dirs.ts @@ -0,0 +1,22 @@ +import type { Dirent } from "node:fs"; +import fs from "node:fs/promises"; +import path from "node:path"; + +export async function resolveAgentSessionDirs(stateDir: string): Promise { + const agentsDir = path.join(stateDir, "agents"); + let entries: Dirent[] = []; + try { + entries = await fs.readdir(agentsDir, { withFileTypes: true }); + } catch (err) { + const code = (err as { code?: string }).code; + if (code === "ENOENT") { + return []; + } + throw err; + } + + return entries + .filter((entry) => entry.isDirectory()) + .map((entry) => path.join(agentsDir, entry.name, "sessions")) + .toSorted((a, b) => a.localeCompare(b)); +} diff --git a/src/commands/doctor-session-locks.ts b/src/commands/doctor-session-locks.ts index d516484a5e..d7331c6cf0 100644 --- a/src/commands/doctor-session-locks.ts +++ b/src/commands/doctor-session-locks.ts @@ -1,6 +1,4 @@ -import type { Dirent } from "node:fs"; -import fs from "node:fs/promises"; -import path from "node:path"; +import { resolveAgentSessionDirs } from "../agents/session-dirs.js"; import { cleanStaleLockFiles, type SessionLockInspection } from "../agents/session-write-lock.js"; import { resolveStateDir } from "../config/paths.js"; import { note } from "../terminal/note.js"; @@ -8,25 +6,6 @@ import { shortenHomePath } from "../utils.js"; const DEFAULT_STALE_MS = 30 * 60 * 1000; -async function resolveAgentSessionDirs(stateDir: string): Promise { - const agentsDir = path.join(stateDir, "agents"); - let entries: Dirent[] = []; - try { - entries = await fs.readdir(agentsDir, { withFileTypes: true }); - } catch (err) { - const code = (err as { code?: string }).code; - if (code === "ENOENT") { - return []; - } - throw err; - } - - return entries - .filter((entry) => entry.isDirectory()) - .map((entry) => path.join(agentsDir, entry.name, "sessions")) - .toSorted((a, b) => a.localeCompare(b)); -} - function formatAge(ageMs: number | null): string { if (ageMs === null) { return "unknown"; diff --git a/src/gateway/server-startup.ts b/src/gateway/server-startup.ts index 89e2e38cf6..61e1b30a05 100644 --- a/src/gateway/server-startup.ts +++ b/src/gateway/server-startup.ts @@ -1,6 +1,3 @@ -import type { Dirent } from "node:fs"; -import fs from "node:fs/promises"; -import path from "node:path"; import type { CliDeps } from "../cli/deps.js"; import type { loadConfig } from "../config/config.js"; import type { loadOpenClawPlugins } from "../plugins/loader.js"; @@ -11,6 +8,7 @@ import { resolveConfiguredModelRef, resolveHooksGmailModel, } from "../agents/model-selection.js"; +import { resolveAgentSessionDirs } from "../agents/session-dirs.js"; import { cleanStaleLockFiles } from "../agents/session-write-lock.js"; import { resolveStateDir } from "../config/paths.js"; import { startGmailWatcher } from "../hooks/gmail-watcher.js"; @@ -31,25 +29,6 @@ import { startGatewayMemoryBackend } from "./server-startup-memory.js"; const SESSION_LOCK_STALE_MS = 30 * 60 * 1000; -async function resolveAgentSessionDirs(stateDir: string): Promise { - const agentsDir = path.join(stateDir, "agents"); - let entries: Dirent[] = []; - try { - entries = await fs.readdir(agentsDir, { withFileTypes: true }); - } catch (err) { - const code = (err as { code?: string }).code; - if (code === "ENOENT") { - return []; - } - throw err; - } - - return entries - .filter((entry) => entry.isDirectory()) - .map((entry) => path.join(agentsDir, entry.name, "sessions")) - .toSorted((a, b) => a.localeCompare(b)); -} - export async function startGatewaySidecars(params: { cfg: ReturnType; pluginRegistry: ReturnType;