import { existsSync } from 'fs'; import { dirname, join, parse } from 'path'; export type EntityType = 'tasks' | 'handoffs' | 'decisions' | 'memory' | 'messages' | 'status'; export function getAgentHubDir(cwd: string = process.cwd()): string { return join(cwd, '.agenthub'); } /** * Walk up from `startCwd` to find the nearest directory that contains an * initialized AgentHub project (`.agenthub/agenthub.config.json`). * Returns the project root, or `undefined` if none is found. * This makes the CLI usable from any subdirectory of the project. */ export function findProjectRoot(startCwd: string = process.cwd()): string | undefined { let dir = startCwd; const { root } = parse(dir); while (true) { if (existsSync(join(dir, '.agenthub', 'agenthub.config.json'))) return dir; if (dir === root) return undefined; dir = dirname(dir); } } export function getEntityDir(cwd: string, type: EntityType): string { return join(getAgentHubDir(cwd), type); } export function getCounterPath(cwd: string): string { return join(getAgentHubDir(cwd), '.counter.json'); } export function getConfigPath(cwd: string): string { return join(getAgentHubDir(cwd), 'agenthub.config.json'); } export function getIndexPath(cwd: string): string { return join(getAgentHubDir(cwd), '.index.sqlite'); } export function getStatusPath(cwd: string): string { return join(getEntityDir(cwd, 'status'), 'latest.md'); }