feat(KB-619): add multi-project CLI support

- Add project-resolver module with CentralCore integration for project resolution
- Add project subcommands: list, add, remove, info with proper CLI integration
- Add --project flag support across all task and settings commands
- Refactor getStore to use project resolution with options pattern
- Update bin.ts command routing to extract and propagate project context
- Add project context propagation via FN_PROJECT environment variable
This commit is contained in:
gsxdsm
2026-03-31 23:41:36 -07:00
parent 0855097235
commit 5a12fc3ab5
10 changed files with 2136 additions and 310 deletions

View File

@@ -1,79 +1,270 @@
/**
* Tests for project.ts commands
*/
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { runProjectList, runProjectAdd, runProjectRemove, runProjectInfo } from "./project.js";
describe("project commands", () => {
let consoleSpy: ReturnType<typeof vi.spyOn>;
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
// Mock dependencies
vi.mock("@fusion/core", async () => {
const actual = await vi.importActual<typeof import("@fusion/core")>("@fusion/core");
return {
...actual,
CentralCore: vi.fn(),
GlobalSettingsStore: vi.fn(),
TaskStore: vi.fn().mockImplementation(() => ({
init: vi.fn().mockResolvedValue(undefined),
listTasks: vi.fn().mockResolvedValue([]),
})),
};
});
beforeEach(() => {
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
});
vi.mock("@fusion/engine", () => ({
ProjectManager: vi.fn().mockImplementation(() => ({
getRuntime: vi.fn().mockReturnValue(undefined),
removeProject: vi.fn().mockResolvedValue(undefined),
})),
}));
afterEach(() => {
consoleSpy.mockRestore();
consoleErrorSpy.mockRestore();
vi.resetModules();
});
vi.mock("../project-resolver.js", () => ({
getCentralCore: vi.fn(),
getProjectManager: vi.fn(),
findKbDir: vi.fn().mockReturnValue(null),
isKbProject: vi.fn().mockReturnValue(true),
suggestProjectName: vi.fn().mockReturnValue("test-project"),
formatLastActivity: vi.fn().mockReturnValue("just now"),
}));
describe("Project Commands", () => {
describe("exports", () => {
it("should export all project command functions", async () => {
const project = await import("./project.js");
expect(typeof project.runProjectList).toBe("function");
expect(typeof project.runProjectAdd).toBe("function");
expect(typeof project.runProjectRemove).toBe("function");
expect(typeof project.runProjectShow).toBe("function");
expect(typeof project.runProjectSetDefault).toBe("function");
expect(typeof project.runProjectDetect).toBe("function");
it("exports runProjectList as a function", () => {
expect(typeof runProjectList).toBe("function");
});
it("exports runProjectAdd as a function", () => {
expect(typeof runProjectAdd).toBe("function");
});
it("exports runProjectRemove as a function", () => {
expect(typeof runProjectRemove).toBe("function");
});
it("exports runProjectInfo as a function", () => {
expect(typeof runProjectInfo).toBe("function");
});
});
describe("validation errors", () => {
it("runProjectAdd should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectAdd } = await import("./project.js");
await expect(runProjectAdd("", "/tmp")).rejects.toThrow("process.exit");
exitSpy.mockRestore();
describe("runProjectList", () => {
it("should handle empty project list", async () => {
const { getCentralCore } = await import("../project-resolver.js");
vi.mocked(getCentralCore).mockResolvedValue({
listProjects: vi.fn().mockResolvedValue([]),
getProjectHealth: vi.fn().mockResolvedValue(undefined),
} as unknown as import("@fusion/core").CentralCore);
const { getProjectManager } = await import("../project-resolver.js");
vi.mocked(getProjectManager).mockResolvedValue({
getRuntime: vi.fn().mockReturnValue(undefined),
} as unknown as import("@fusion/engine").ProjectManager);
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await runProjectList();
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("No projects registered"));
consoleSpy.mockRestore();
});
it("runProjectAdd should exit when path is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
it("should output JSON when --json flag is set", async () => {
const mockProject = {
id: "proj_123",
name: "test-project",
path: "/path/to/project",
status: "active",
isolationMode: "in-process",
createdAt: "2024-01-01T00:00:00.000Z",
updatedAt: "2024-01-01T00:00:00.000Z",
};
const { getCentralCore } = await import("../project-resolver.js");
vi.mocked(getCentralCore).mockResolvedValue({
listProjects: vi.fn().mockResolvedValue([mockProject]),
getProjectHealth: vi.fn().mockResolvedValue({
lastActivityAt: "2024-01-01T00:00:00.000Z",
inFlightAgentCount: 0,
}),
} as unknown as import("@fusion/core").CentralCore);
const consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await runProjectList({ json: true });
// Check that JSON was output
const jsonCall = consoleSpy.mock.calls.find(call => {
try {
JSON.parse(call[0] as string);
return true;
} catch {
return false;
}
});
const { runProjectAdd } = await import("./project.js");
await expect(runProjectAdd("name", "")).rejects.toThrow("process.exit");
expect(jsonCall).toBeDefined();
const output = JSON.parse(jsonCall![0] as string);
expect(output).toBeInstanceOf(Array);
expect(output[0]).toHaveProperty("id", "proj_123");
expect(output[0]).toHaveProperty("name", "test-project");
consoleSpy.mockRestore();
});
});
describe("runProjectAdd", () => {
it("should exit if no directory provided in non-interactive mode", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => {}) as (code?: number) => never);
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
await runProjectAdd(undefined, { interactive: false });
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Usage:"));
expect(exitSpy).toHaveBeenCalledWith(1);
exitSpy.mockRestore();
errorSpy.mockRestore();
});
it("runProjectRemove should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectRemove } = await import("./project.js");
await expect(runProjectRemove("")).rejects.toThrow("process.exit");
it("should validate isolation mode", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => {}) as (code?: number) => never);
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
await runProjectAdd("/tmp", { isolation: "invalid-mode" as any, interactive: false });
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("Invalid isolation mode"));
expect(exitSpy).toHaveBeenCalledWith(1);
exitSpy.mockRestore();
errorSpy.mockRestore();
});
});
describe("runProjectRemove", () => {
it("should exit if project not found", async () => {
const { getCentralCore } = await import("../project-resolver.js");
vi.mocked(getCentralCore).mockResolvedValue({
listProjects: vi.fn().mockResolvedValue([]),
getProject: vi.fn().mockResolvedValue(undefined),
getProjectByPath: vi.fn().mockResolvedValue(undefined),
unregisterProject: vi.fn(),
} as unknown as import("@fusion/core").CentralCore);
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => {}) as (code?: number) => never);
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
await runProjectRemove("nonexistent", { force: true, interactive: false });
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("not found"));
expect(exitSpy).toHaveBeenCalledWith(1);
exitSpy.mockRestore();
errorSpy.mockRestore();
});
it("runProjectShow should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectShow } = await import("./project.js");
await expect(runProjectShow("")).rejects.toThrow("process.exit");
exitSpy.mockRestore();
it("should skip confirmation with --force flag", async () => {
const mockProject = {
id: "proj_123",
name: "test-project",
path: "/path/to/project",
};
const { getCentralCore } = await import("../project-resolver.js");
vi.mocked(getCentralCore).mockResolvedValue({
listProjects: vi.fn().mockResolvedValue([mockProject]),
getProject: vi.fn().mockResolvedValue(mockProject),
getProjectByPath: vi.fn().mockResolvedValue(mockProject),
unregisterProject: vi.fn().mockResolvedValue(undefined),
} as unknown as import("@fusion/core").CentralCore);
const { getProjectManager } = await import("../project-resolver.js");
vi.mocked(getProjectManager).mockResolvedValue({
getRuntime: vi.fn().mockReturnValue(undefined),
removeProject: vi.fn().mockResolvedValue(undefined),
} as unknown as import("@fusion/engine").ProjectManager);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await runProjectRemove("test-project", { force: true, interactive: false });
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Unregistered"));
logSpy.mockRestore();
});
});
describe("runProjectInfo", () => {
it("should auto-detect project from cwd when no name provided", async () => {
const mockProject = {
id: "proj_123",
name: "detected-project",
path: "/current/dir",
status: "active",
isolationMode: "in-process",
createdAt: "2024-01-01T00:00:00.000Z",
updatedAt: "2024-01-01T00:00:00.000Z",
};
const { getCentralCore, findKbDir } = await import("../project-resolver.js");
vi.mocked(findKbDir).mockReturnValue("/current/dir");
vi.mocked(getCentralCore).mockResolvedValue({
listProjects: vi.fn().mockResolvedValue([mockProject]),
getProject: vi.fn().mockResolvedValue(mockProject),
getProjectByPath: vi.fn().mockResolvedValue(mockProject),
getProjectHealth: vi.fn().mockResolvedValue({
activeTaskCount: 5,
inFlightAgentCount: 2,
totalTasksCompleted: 100,
totalTasksFailed: 5,
lastActivityAt: "2024-01-01T00:00:00.000Z",
}),
} as unknown as import("@fusion/core").CentralCore);
const { getProjectManager } = await import("../project-resolver.js");
vi.mocked(getProjectManager).mockResolvedValue({
getRuntime: vi.fn().mockReturnValue({ getStatus: () => "active" }),
removeProject: vi.fn(),
} as unknown as import("@fusion/engine").ProjectManager);
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await runProjectInfo(undefined, { interactive: false });
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("detected-project"));
logSpy.mockRestore();
});
it("runProjectSetDefault should exit when name is empty", async () => {
const exitSpy = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
const { runProjectSetDefault } = await import("./project.js");
await expect(runProjectSetDefault("")).rejects.toThrow("process.exit");
it("should exit if project not found by name", async () => {
const { getCentralCore } = await import("../project-resolver.js");
vi.mocked(getCentralCore).mockResolvedValue({
listProjects: vi.fn().mockResolvedValue([]),
getProject: vi.fn().mockResolvedValue(undefined),
} as unknown as import("@fusion/core").CentralCore);
const exitSpy = vi.spyOn(process, "exit").mockImplementation((() => {}) as (code?: number) => never);
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
await runProjectInfo("nonexistent", { interactive: false });
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining("not found"));
expect(exitSpy).toHaveBeenCalledWith(1);
exitSpy.mockRestore();
errorSpy.mockRestore();
});
});
});
// Helper function for creating mock CentralCore
describe("Project command helpers", () => {
it("should export all required functions", () => {
expect(runProjectList).toBeDefined();
expect(runProjectAdd).toBeDefined();
expect(runProjectRemove).toBeDefined();
expect(runProjectInfo).toBeDefined();
});
});

View File

@@ -1,67 +1,200 @@
/**
* Project command implementations for kb CLI.
* Project subcommand implementations for kb CLI.
*
* Implements:
* - fn project list [--json]
* - fn project add [dir] [--name <name>] [--isolation <mode>]
* - fn project remove <name> [--force]
* - fn project info [name]
*/
import { CentralCore, GlobalSettingsStore, type RegisteredProject, type IsolationMode } from "@fusion/core";
import { resolve, isAbsolute } from "node:path";
import { CentralCore, type RegisteredProject, type IsolationMode } from "@fusion/core";
import { resolve, isAbsolute, basename } from "node:path";
import { existsSync, statSync } from "node:fs";
import { createInterface } from "node:readline/promises";
import { formatProjectLine, detectProjectFromCwd, setDefaultProject } from "../project-context.js";
import {
getCentralCore,
getProjectManager,
findKbDir,
isKbProject,
suggestProjectName,
formatLastActivity,
type ResolvedProject,
} from "../project-resolver.js";
const VALID_ISOLATION_MODES: IsolationMode[] = ["in-process", "child-process"];
export async function runProjectList(): Promise<void> {
const central = new CentralCore();
await central.init();
/**
* Run the `fn project list` command.
*
* Shows all registered projects with:
* - Name, directory, status
* - In-flight task count
* - Last activity timestamp
* - Optional JSON output with --json flag
*/
export async function runProjectList(options: { json?: boolean } = {}): Promise<void> {
const central = await getCentralCore();
const pm = await getProjectManager();
try {
const projects = await central.listProjects();
const defaultProject = await getDefaultProject();
const projects = await central.listProjects();
if (projects.length === 0) {
if (projects.length === 0) {
if (options.json) {
console.log(JSON.stringify([], null, 2));
} else {
console.log("\n No projects registered.");
console.log(" Register one with: kb project add <name> <path>\n");
return;
console.log(" Register one with: fn project add <path>\n");
}
return;
}
// Get detailed info for each project
const projectsWithInfo = await Promise.all(
projects.map(async (project) => {
const runtime = pm.getRuntime(project.id);
const runtimeStatus = runtime?.getStatus() ?? "not_started";
// Get task counts from store
let taskCounts: Record<string, number> = {};
let totalTasks = 0;
try {
const { TaskStore } = await import("@fusion/core");
const store = new TaskStore(project.path);
await store.init();
const tasks = await store.listTasks();
totalTasks = tasks.length;
for (const task of tasks) {
taskCounts[task.column] = (taskCounts[task.column] || 0) + 1;
}
} catch {
// Ignore errors reading tasks
}
const health = await central.getProjectHealth(project.id);
return {
project,
runtimeStatus,
taskCounts,
totalTasks,
lastActivity: health?.lastActivityAt,
activeAgents: health?.inFlightAgentCount ?? 0,
};
})
);
// Sort by name alphabetically
projectsWithInfo.sort((a, b) => a.project.name.localeCompare(b.project.name));
if (options.json) {
// JSON output
const jsonOutput = projectsWithInfo.map((p) => ({
id: p.project.id,
name: p.project.name,
path: p.project.path,
status: p.project.status,
isolationMode: p.project.isolationMode,
runtimeStatus: p.runtimeStatus,
totalTasks: p.totalTasks,
taskCounts: p.taskCounts,
activeAgents: p.activeAgents,
lastActivity: p.lastActivity,
createdAt: p.project.createdAt,
updatedAt: p.project.updatedAt,
}));
console.log(JSON.stringify(jsonOutput, null, 2));
} else {
// Table output
console.log();
console.log(" Registered Projects:");
console.log();
for (const project of projects) {
const isDefault = defaultProject?.id === project.id;
const line = formatProjectLine(project, isDefault);
console.log(` ${line}`);
// Calculate column widths
const nameWidth = Math.max(...projectsWithInfo.map((p) => p.project.name.length), 4);
const pathWidth = Math.max(...projectsWithInfo.map((p) => p.project.path.length), 4);
// Header
console.log(
` ${"Name".padEnd(nameWidth)} ${"Path".padEnd(pathWidth)} ${"Status".padEnd(10)} ${"Tasks".padEnd(6)} ${"Agents".padEnd(6)} Last Activity`
);
console.log(
` ${"-".repeat(nameWidth)} ${"-".repeat(pathWidth)} ${"-".repeat(10)} ${"-".repeat(6)} ${"-".repeat(6)} -------------`
);
for (const p of projectsWithInfo) {
const statusIcon = getStatusIcon(p.project.status);
const lastActivity = formatLastActivity(p.lastActivity);
console.log(
` ${p.project.name.padEnd(nameWidth)} ${p.project.path.padEnd(pathWidth)} ${statusIcon} ${p.project.status.padEnd(8)} ${String(p.totalTasks).padEnd(6)} ${String(p.activeAgents).padEnd(6)} ${lastActivity}`
);
}
console.log();
const activeCount = projects.filter((p) => p.status === "active").length;
const activeCount = projectsWithInfo.filter((p) => p.project.status === "active").length;
console.log(` ${projects.length} project${projects.length === 1 ? "" : "s"} registered, ${activeCount} active`);
if (defaultProject) {
console.log(` * indicates default project (${defaultProject.name})`);
}
console.log();
} finally {
await central.close();
}
}
/**
* Run the `fn project add` command.
*
* Registers a new project with optional interactive prompts.
*/
export async function runProjectAdd(
name: string,
path: string,
options?: { isolation?: string; force?: boolean }
dir?: string,
options: { name?: string; isolation?: "in-process" | "child-process"; interactive?: boolean } = {}
): Promise<void> {
if (!name || !path) {
console.error("Usage: kb project add <name> <path> [--isolation <mode>]");
const central = await getCentralCore();
const interactive = options.interactive ?? true;
// Interactive wizard if no directory provided
if (!dir && interactive) {
const rl = createInterface({ input: process.stdin, output: process.stdout });
// Ask for directory
const cwd = process.cwd();
const dirInput = await rl.question(` Project directory [${cwd}]: `);
dir = dirInput.trim() || cwd;
// Check if directory has .kb/
const kbPath = resolve(dir, ".kb");
if (!existsSync(kbPath)) {
console.log(`\n No .kb/ directory found in ${dir}`);
const shouldInit = await promptConfirm("Initialize kb here first?", true);
if (shouldInit) {
const { TaskStore } = await import("@fusion/core");
const store = new TaskStore(dir);
await store.init();
console.log(` ✓ Initialized kb at ${dir}`);
} else {
console.log(" Cannot register project without .kb/ directory.");
rl.close();
process.exit(1);
}
}
// Ask for name
const suggestedName = options.name || suggestProjectName(dir);
const nameInput = await rl.question(` Project name [${suggestedName}]: `);
options.name = nameInput.trim() || suggestedName;
// Ask for isolation mode
const isolationInput = await rl.question(` Isolation mode [in-process]: `);
options.isolation = (isolationInput.trim() as IsolationMode) || "in-process";
rl.close();
}
if (!dir) {
console.error("Usage: fn project add [dir] [--name <name>] [--isolation <mode>]");
process.exit(1);
}
if (!isValidProjectName(name)) {
console.error(`Error: Invalid project name '${name}'`);
process.exit(1);
}
const absolutePath = isAbsolute(path) ? path : resolve(process.cwd(), path);
// Resolve and validate directory
const absolutePath = isAbsolute(dir) ? dir : resolve(process.cwd(), dir);
if (!existsSync(absolutePath)) {
console.error(`Error: Path does not exist: ${absolutePath}`);
@@ -73,172 +206,222 @@ export async function runProjectAdd(
process.exit(1);
}
const kbDbPath = resolve(absolutePath, ".kb", "kb.db");
if (!existsSync(kbDbPath) && !options?.force) {
// Check for .kb/ directory
if (!isKbProject(absolutePath)) {
console.error(`Error: No kb project found at ${absolutePath}`);
console.error("Run `fn init` first to initialize a kb project.");
process.exit(1);
}
const isolationMode = options?.isolation as IsolationMode | undefined;
if (isolationMode && !VALID_ISOLATION_MODES.includes(isolationMode)) {
// Validate isolation mode
const isolationMode = options.isolation ?? "in-process";
if (!VALID_ISOLATION_MODES.includes(isolationMode)) {
console.error(`Error: Invalid isolation mode '${isolationMode}'`);
console.error(`Valid modes: ${VALID_ISOLATION_MODES.join(", ")}`);
process.exit(1);
}
const central = new CentralCore();
await central.init();
// Determine project name
const name = options.name || suggestProjectName(absolutePath);
try {
const existing = await findProjectByName(central, name);
if (existing) {
console.error(`Error: Project '${name}' already registered.`);
process.exit(1);
}
const project = await central.registerProject({
name,
path: absolutePath,
isolationMode: isolationMode ?? "in-process",
});
console.log();
console.log(` ✓ Registered project '${name}'`);
console.log(` ID: ${project.id}`);
console.log();
} finally {
await central.close();
// Check for duplicate name
const existing = await findProjectByName(central, name);
if (existing) {
console.error(`Error: Project '${name}' already registered.`);
process.exit(1);
}
// Check for duplicate path
const existingByPath = await central.getProjectByPath(absolutePath);
if (existingByPath) {
console.error(`Error: Project already registered at path: ${absolutePath}`);
console.error(`Existing project: ${existingByPath.name}`);
process.exit(1);
}
// Register the project
const project = await central.registerProject({
name,
path: absolutePath,
isolationMode,
});
console.log();
console.log(` ✓ Registered project '${name}'`);
console.log(` ID: ${project.id}`);
console.log(` Path: ${project.path}`);
console.log(` Isolation: ${project.isolationMode}`);
console.log();
}
export async function runProjectRemove(name: string, force?: boolean): Promise<void> {
/**
* Run the `fn project remove` command.
*
* Unregisters a project from the central registry.
*/
export async function runProjectRemove(
name: string,
options: { force?: boolean; interactive?: boolean } = {}
): Promise<void> {
const central = await getCentralCore();
const pm = await getProjectManager();
const interactive = options.interactive ?? true;
if (!name) {
console.error("Usage: kb project remove <name> [--force]");
console.error("Usage: fn project remove <name> [--force]");
process.exit(1);
}
const central = new CentralCore();
await central.init();
const project = await findProjectByNameOrId(central, name);
if (!project) {
console.error(`Error: Project '${name}' not found.`);
process.exit(1);
}
try {
const project = await findProjectByNameOrId(central, name);
if (!project) {
// Check if runtime is active
const runtime = pm.getRuntime(project.id);
if (runtime) {
console.log(` Stopping runtime for '${project.name}'...`);
await pm.removeProject(project.id);
}
// Confirmation prompt
if (!options.force && interactive) {
const confirmed = await promptConfirm(
`Unregister "${project.name}"? Project data will be preserved, only the registry entry will be removed.`,
false
);
if (!confirmed) {
console.log("Cancelled.");
return;
}
}
await central.unregisterProject(project.id);
console.log();
console.log(` ✓ Unregistered project '${project.name}'`);
console.log(` Project data at ${project.path} is preserved.`);
console.log();
}
/**
* Run the `fn project info` command.
*
* Shows detailed information about a specific project.
*/
export async function runProjectInfo(name?: string, options: { interactive?: boolean } = {}): Promise<void> {
const central = await getCentralCore();
const pm = await getProjectManager();
const interactive = options.interactive ?? true;
let project: RegisteredProject;
if (name) {
const found = await findProjectByNameOrId(central, name);
if (!found) {
console.error(`Error: Project '${name}' not found.`);
process.exit(1);
}
project = found;
} else {
// Auto-detect from cwd
const cwd = process.cwd();
const kbDir = findKbDir(cwd);
if (!force) {
const rl = createInterface({ input: process.stdin, output: process.stdout });
const answer = await rl.question(`Unregister project '${project.name}'? [y/N] `);
rl.close();
if (kbDir) {
const found = await central.getProjectByPath(kbDir);
if (found) {
project = found;
} else {
console.error(`Error: Found kb project at ${kbDir} but it's not registered.`);
console.error("Run `fn project add .` to register it.");
process.exit(1);
}
} else {
// List projects and ask user to select
const projects = await central.listProjects();
if (projects.length === 0) {
console.error("Error: No projects registered.");
process.exit(1);
}
if (answer.trim().toLowerCase() !== "y") {
console.log("Cancelled.");
return;
if (projects.length === 1) {
project = projects[0];
} else if (interactive) {
project = await promptProjectSelection(projects, "Select a project:");
} else {
console.error("Error: Multiple projects registered. Please specify a project name.");
console.error("Run `fn project list` to see available projects.");
process.exit(1);
}
}
await central.unregisterProject(project.id);
console.log(` ✓ Unregistered project '${project.name}'`);
} finally {
await central.close();
}
}
export async function runProjectShow(name: string): Promise<void> {
if (!name) {
console.error("Usage: kb project show <name>");
process.exit(1);
}
const central = new CentralCore();
await central.init();
// Get runtime status
const runtime = pm.getRuntime(project.id);
const runtimeStatus = runtime?.getStatus() ?? "not_started";
// Get task counts
let taskCounts: Record<string, number> = {};
let totalTasks = 0;
try {
const project = await findProjectByNameOrId(central, name);
if (!project) {
console.error(`Error: Project '${name}' not found.`);
process.exit(1);
const { TaskStore } = await import("@fusion/core");
const store = new TaskStore(project.path);
await store.init();
const tasks = await store.listTasks();
totalTasks = tasks.length;
for (const task of tasks) {
taskCounts[task.column] = (taskCounts[task.column] || 0) + 1;
}
const defaultProject = await getDefaultProject();
const isDefault = defaultProject?.id === project.id;
console.log();
console.log(` Project: ${project.name}${isDefault ? " (default)" : ""}`);
console.log(` ID: ${project.id}`);
console.log(` Path: ${project.path}`);
console.log(` Status: ${project.status}`);
console.log();
} finally {
await central.close();
}
}
export async function runProjectSetDefault(name: string): Promise<void> {
if (!name) {
console.error("Usage: kb project set-default <name>");
process.exit(1);
} catch {
// Ignore errors reading tasks
}
const central = new CentralCore();
await central.init();
// Get health metrics
const health = await central.getProjectHealth(project.id);
try {
const project = await findProjectByNameOrId(central, name);
if (!project) {
console.error(`Error: Project '${name}' not found.`);
process.exit(1);
// Display info
console.log();
console.log(` Project: ${project.name}`);
console.log(` ID: ${project.id}`);
console.log(` Path: ${project.path}`);
console.log(` Status: ${project.status}`);
console.log(` Isolation Mode: ${project.isolationMode}`);
console.log(` Runtime: ${runtimeStatus}`);
console.log(` Created: ${new Date(project.createdAt).toLocaleString()}`);
console.log(` Updated: ${new Date(project.updatedAt).toLocaleString()}`);
console.log();
console.log(` Tasks (${totalTasks} total):`);
const columns = ["triage", "todo", "in-progress", "in-review", "done", "archived"];
for (const col of columns) {
const count = taskCounts[col] || 0;
if (count > 0 || col !== "archived") {
const icon = getColumnIcon(col);
console.log(` ${icon} ${col}: ${count}`);
}
await setDefaultProject(project.id);
console.log();
console.log(` ✓ Set '${project.name}' as default project`);
console.log();
} finally {
await central.close();
}
}
console.log();
export async function runProjectDetect(): Promise<void> {
const central = new CentralCore();
await central.init();
try {
const project = await detectProjectFromCwd(process.cwd(), central);
if (project) {
console.log();
console.log(` Detected: ${project.name} (${project.path})`);
console.log();
} else {
console.log();
console.log(" No kb project detected from current directory.");
console.log();
if (health) {
console.log(" Activity:");
console.log(` Active tasks: ${health.activeTaskCount}`);
console.log(` In-flight agents: ${health.inFlightAgentCount}`);
console.log(` Total completed: ${health.totalTasksCompleted}`);
console.log(` Total failed: ${health.totalTasksFailed}`);
if (health.lastActivityAt) {
console.log(` Last activity: ${formatLastActivity(health.lastActivityAt)}`);
}
} finally {
await central.close();
if (health.averageTaskDurationMs) {
const avgMins = Math.round(health.averageTaskDurationMs / 60000);
console.log(` Avg task duration: ${avgMins}m`);
}
console.log();
}
}
// Helpers
async function getDefaultProject(): Promise<RegisteredProject | undefined> {
const globalStore = new GlobalSettingsStore();
await globalStore.init();
const settings = await globalStore.getSettings();
if (!settings.defaultProjectId) {
return undefined;
}
const central = new CentralCore();
await central.init();
try {
return await central.getProject(settings.defaultProjectId);
} finally {
await central.close();
}
}
// Helper functions
async function findProjectByName(central: CentralCore, name: string): Promise<RegisteredProject | undefined> {
const allProjects = await central.listProjects();
@@ -247,16 +430,81 @@ async function findProjectByName(central: CentralCore, name: string): Promise<Re
}
async function findProjectByNameOrId(central: CentralCore, nameOrId: string): Promise<RegisteredProject | undefined> {
// First try exact ID match
const byId = await central.getProject(nameOrId);
if (byId) {
return byId;
}
// Then try case-insensitive name match
return findProjectByName(central, nameOrId);
}
function isValidProjectName(name: string): boolean {
if (!name || name.length < 1 || name.length > 64) {
return false;
async function promptProjectSelection(
projects: RegisteredProject[],
message: string
): Promise<RegisteredProject> {
const rl = createInterface({ input: process.stdin, output: process.stdout });
console.log(`\n ${message}`);
for (let i = 0; i < projects.length; i++) {
console.log(` ${i + 1}. ${projects[i].name} (${projects[i].path})`);
}
while (true) {
const answer = await rl.question("\n Enter number: ");
const num = parseInt(answer.trim(), 10);
if (!isNaN(num) && num >= 1 && num <= projects.length) {
rl.close();
return projects[num - 1];
}
console.log(` Invalid selection. Please enter a number between 1 and ${projects.length}`);
}
}
async function promptConfirm(message: string, defaultYes = false): Promise<boolean> {
const rl = createInterface({ input: process.stdin, output: process.stdout });
const prompt = defaultYes ? "[Y/n]" : "[y/N]";
const answer = await rl.question(` ${message} ${prompt}: `);
rl.close();
const trimmed = answer.trim().toLowerCase();
if (trimmed === "" && defaultYes) return true;
return trimmed === "y" || trimmed === "yes";
}
function getStatusIcon(status: string): string {
switch (status) {
case "active":
return "●";
case "paused":
return "⏸";
case "errored":
return "✗";
case "initializing":
return "◌";
default:
return "○";
}
}
function getColumnIcon(column: string): string {
switch (column) {
case "triage":
return "●";
case "todo":
return "○";
case "in-progress":
return "▸";
case "in-review":
return "◆";
case "done":
return "✓";
case "archived":
return "▪";
default:
return "•";
}
return /^[a-zA-Z0-9_-]+$/.test(name);
}

View File

@@ -15,13 +15,74 @@ vi.mock("@fusion/core", () => {
githubTokenConfigured: false,
};
// Mock CentralCore for project-resolver
const mockCentralCore = vi.fn().mockImplementation(() => ({
init: vi.fn().mockResolvedValue(undefined),
close: vi.fn().mockResolvedValue(undefined),
listProjects: vi.fn().mockResolvedValue([]),
getProject: vi.fn().mockResolvedValue(undefined),
getProjectByPath: vi.fn().mockResolvedValue(undefined),
registerProject: vi.fn(),
unregisterProject: vi.fn(),
getProjectHealth: vi.fn().mockResolvedValue(undefined),
isInitialized: vi.fn().mockReturnValue(true),
}));
return {
TaskStore: vi.fn(),
CentralCore: mockCentralCore,
DEFAULT_SETTINGS,
};
});
// Mock project-resolver to return a simple getStore that returns a mock store
vi.mock("../project-resolver.js", async () => {
// Create a mock store with the methods tests expect
const createMockStore = () => ({
init: vi.fn().mockResolvedValue(undefined),
getSettings: vi.fn().mockResolvedValue({
maxConcurrent: 2,
maxWorktrees: 4,
autoResolveConflicts: true,
smartConflictResolution: true,
requirePlanApproval: false,
ntfyEnabled: false,
taskPrefix: undefined,
ntfyTopic: undefined,
worktreeNaming: "random",
githubTokenConfigured: false,
defaultProvider: undefined,
defaultModelId: undefined,
defaultThinkingLevel: undefined,
}),
updateSettings: vi.fn().mockResolvedValue(undefined),
});
return {
getStore: vi.fn().mockImplementation(createMockStore),
resolveProject: vi.fn().mockRejectedValue(new Error("Not implemented in mock")),
ProjectResolutionError: class ProjectResolutionError extends Error {
code: string;
context?: Record<string, unknown>;
constructor(message: string, code: string, context?: Record<string, unknown>) {
super(message);
this.name = "ProjectResolutionError";
this.code = code;
this.context = context;
}
},
getCentralCore: vi.fn(),
getProjectManager: vi.fn(),
findKbDir: vi.fn().mockReturnValue(null),
isKbProject: vi.fn().mockReturnValue(true),
suggestProjectName: vi.fn().mockReturnValue("test-project"),
formatLastActivity: vi.fn().mockReturnValue("just now"),
resetProjectResolution: vi.fn(),
};
});
import { TaskStore, DEFAULT_SETTINGS } from "@fusion/core";
import { getStore } from "../project-resolver.js";
import {
runSettingsShow,
runSettingsSet,
@@ -223,10 +284,10 @@ describe("runSettingsShow", () => {
taskPrefix: "CUSTOM",
});
(TaskStore as unknown as ReturnType<typeof vi.fn>).mockImplementation(() => ({
(getStore as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
init: vi.fn(),
getSettings: vi.fn().mockResolvedValue(mockSettings),
}));
});
await runSettingsShow();
@@ -254,10 +315,10 @@ describe("runSettingsShow", () => {
githubTokenConfigured: true,
});
(TaskStore as unknown as ReturnType<typeof vi.fn>).mockImplementation(() => ({
(getStore as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
init: vi.fn(),
getSettings: vi.fn().mockResolvedValue(mockSettings),
}));
});
await runSettingsShow();
@@ -272,10 +333,10 @@ describe("runSettingsShow", () => {
githubTokenConfigured: false,
});
(TaskStore as unknown as ReturnType<typeof vi.fn>).mockImplementation(() => ({
(getStore as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
init: vi.fn(),
getSettings: vi.fn().mockResolvedValue(mockSettings),
}));
});
await runSettingsShow();
@@ -302,7 +363,7 @@ describe("runSettingsSet", () => {
maxWorktrees: 4,
});
(TaskStore as unknown as ReturnType<typeof vi.fn>).mockImplementation(() => ({
(getStore as unknown as ReturnType<typeof vi.fn>).mockResolvedValue({
init: vi.fn(),
updateSettings: mockUpdateSettings,
getSettings: vi.fn().mockResolvedValue({
@@ -310,7 +371,7 @@ describe("runSettingsSet", () => {
maxWorktrees: 4,
taskPrefix: "TEST",
}),
}));
});
});
afterEach(() => {

View File

@@ -1,5 +1,5 @@
import { TaskStore, type Settings, DEFAULT_SETTINGS } from "@fusion/core";
import { getStore as getStoreFromContext } from "../project-context.js";
import { getStore } from "../project-resolver.js";
// Settings that can be updated via CLI
export const VALID_SETTINGS = [
@@ -39,15 +39,6 @@ const NUMBER_RANGES: Record<string, { min: number; max: number }> = {
maxWorktrees: { min: 1, max: 20 },
};
async function getStore(projectName?: string): Promise<TaskStore> {
if (projectName) {
return getStoreFromContext(projectName);
}
const store = new TaskStore(process.cwd());
await store.init();
return store;
}
/**
* Parse and validate a setting value based on its key's expected type
*/

View File

@@ -7,19 +7,10 @@ import { watchFile, unwatchFile, statSync, existsSync, readFileSync } from "node
import { join } from "node:path";
import { GitHubClient } from "@fusion/dashboard";
import { isGhAvailable, isGhAuthenticated, getCurrentRepo } from "@fusion/core/gh-cli";
import { resolveProject, getStore as getStoreFromContext } from "../project-context.js";
import { getStore, resolveProject } from "../project-resolver.js";
const STEP_STATUSES: StepStatus[] = ["pending", "in-progress", "done", "skipped"];
async function getStore(projectName?: string): Promise<TaskStore> {
if (projectName) {
return getStoreFromContext(projectName);
}
const store = new TaskStore(process.cwd());
await store.init();
return store;
}
export async function runTaskCreate(descriptionArg?: string, attachFiles?: string[], depends?: string[], projectName?: string) {
let description = descriptionArg;
@@ -34,7 +25,7 @@ export async function runTaskCreate(descriptionArg?: string, attachFiles?: strin
process.exit(1);
}
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.createTask({ description: description.trim(), dependencies: depends });
const label = task.description.length > 60
@@ -82,7 +73,7 @@ export async function runTaskCreate(descriptionArg?: string, attachFiles?: strin
}
export async function runTaskList(projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const tasks = await store.listTasks();
if (tasks.length === 0) {
@@ -127,7 +118,7 @@ export async function runTaskUpdate(id: string, stepStr: string, status: string,
process.exit(1);
}
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.updateStep(id, stepIndex, status as StepStatus);
const step = task.steps[stepIndex];
@@ -138,7 +129,7 @@ export async function runTaskUpdate(id: string, stepStr: string, status: string,
}
export async function runTaskLog(id: string, message: string, outcome?: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
await store.logEntry(id, message, outcome);
console.log();
@@ -220,7 +211,7 @@ function filterEntries(entries: AgentLogEntry[], options: LogsOptions): AgentLog
}
export async function runTaskLogs(id: string, options: LogsOptions = {}, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
// Verify task exists
try {
@@ -244,8 +235,8 @@ export async function runTaskLogs(id: string, options: LogsOptions = {}, project
// Follow mode: watch for new entries
if (options.follow) {
const store = await getStore(projectName);
const projectPath = (await resolveProject(projectName)).projectPath;
const store = await getStore({ project: projectName });
const projectPath = (await resolveProject({ project: projectName })).directory;
const logPath = join(projectPath, ".fusion", "tasks", id, "agent.log");
if (!existsSync(logPath)) {
@@ -326,7 +317,7 @@ export async function runTaskLogs(id: string, options: LogsOptions = {}, project
}
export async function runTaskShow(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.getTask(id);
console.log();
@@ -365,13 +356,13 @@ export async function runTaskShow(id: string, projectName?: string) {
}
export async function runTaskMerge(id: string, projectName?: string) {
const store = await getStore(projectName);
const { projectPath } = await resolveProject(projectName);
const store = await getStore({ project: projectName });
const { directory } = await resolveProject({ project: projectName });
console.log(`\n Merging ${id} with AI...\n`);
try {
const result = await aiMergeTask(store, projectPath, id, {
const result = await aiMergeTask(store, directory, id, {
onAgentText: (delta) => process.stdout.write(delta),
});
@@ -432,7 +423,7 @@ export async function runTaskAttach(id: string, filePath: string, projectName?:
process.exit(1);
}
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const attachment = await store.addAttachment(id, filename, content, mimeType);
const sizeKB = (attachment.size / 1024).toFixed(1);
@@ -444,7 +435,7 @@ export async function runTaskAttach(id: string, filePath: string, projectName?:
}
export async function runTaskPause(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.pauseTask(id, true);
console.log();
@@ -453,7 +444,7 @@ export async function runTaskPause(id: string, projectName?: string) {
}
export async function runTaskUnpause(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.pauseTask(id, false);
console.log();
@@ -468,7 +459,7 @@ export async function runTaskMove(id: string, column: string, projectName?: stri
process.exit(1);
}
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.moveTask(id, column as Column);
console.log();
@@ -477,7 +468,7 @@ export async function runTaskMove(id: string, column: string, projectName?: stri
}
export async function runTaskDuplicate(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const newTask = await store.duplicateTask(id);
console.log();
@@ -487,7 +478,7 @@ export async function runTaskDuplicate(id: string, projectName?: string) {
}
export async function runTaskRefine(id: string, feedbackArg?: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
// Get feedback interactively only if not provided (undefined)
let feedback = feedbackArg;
@@ -519,7 +510,7 @@ export async function runTaskRefine(id: string, feedbackArg?: string, projectNam
}
export async function runTaskArchive(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.archiveTask(id);
console.log();
@@ -528,7 +519,7 @@ export async function runTaskArchive(id: string, projectName?: string) {
}
export async function runTaskUnarchive(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.unarchiveTask(id);
console.log();
@@ -537,7 +528,7 @@ export async function runTaskUnarchive(id: string, projectName?: string) {
}
export async function runTaskRetry(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
// Fetch task and validate it exists
let task;
@@ -567,7 +558,7 @@ export async function runTaskRetry(id: string, projectName?: string) {
}
export async function runTaskDelete(id: string, force?: boolean, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
// Check if task exists first
let task;
@@ -620,7 +611,7 @@ export async function runTaskImportGitHubInteractive(
console.log(`\n Fetching issues from ${owner}/${repo}...\n`);
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const existingTasks = await store.listTasks();
// Build a set of already-imported issue URLs
@@ -838,7 +829,7 @@ export async function runTaskImportFromGitHub(
console.log(`\n Importing issues from ${owner}/${repo}...\n`);
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const existingTasks = await store.listTasks();
// Build a set of already-imported issue URLs
@@ -902,7 +893,7 @@ export async function runTaskImportFromGitHub(
}
export async function runTaskComment(id: string, message?: string, author = "user", projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
let text = message;
if (text === undefined) {
@@ -934,7 +925,7 @@ export async function runTaskComment(id: string, message?: string, author = "use
}
export async function runTaskComments(id: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
const task = await store.getTask(id);
const comments = task.comments || [];
@@ -954,7 +945,7 @@ export async function runTaskComments(id: string, projectName?: string) {
}
export async function runTaskSteer(id: string, message?: string, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
// Get message interactively if not provided as argument
let text = message;
@@ -1005,7 +996,7 @@ export interface PrCreateOptions {
}
export async function runTaskPrCreate(id: string, options: PrCreateOptions = {}, projectName?: string) {
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
// Fetch task and validate it exists
let task;
@@ -1341,7 +1332,7 @@ export async function runTaskPlan(initialPlanArg?: string, yesFlag = false, proj
}
}
const store = await getStore(projectName);
const store = await getStore({ project: projectName });
// Create planning session
let sessionId: string;
@@ -1381,7 +1372,7 @@ export async function runTaskPlan(initialPlanArg?: string, yesFlag = false, proj
try {
while (!cancelled) {
// Get user response based on question type
let response: unknown;
let response: Record<string, unknown>;
try {
switch (currentQuestion.type) {