diff --git a/src/cli/commands/delegate.ts b/src/cli/commands/delegate.ts index a9bcdce..9d3b1c0 100644 --- a/src/cli/commands/delegate.ts +++ b/src/cli/commands/delegate.ts @@ -1,6 +1,8 @@ +import { loadConfig } from '../../core/config.js'; import { suggestDelegation, autoDelegate } from '../../core/services/delegateService.js'; export async function delegate(cwd: string, options: { auto?: boolean } = {}): Promise { + const config = loadConfig(cwd); const suggestion = suggestDelegation(cwd); if (!suggestion) { console.log('No open tasks to delegate.'); @@ -12,7 +14,7 @@ export async function delegate(cwd: string, options: { auto?: boolean } = {}): P console.log(` Role: ${suggestion.role}`); console.log(` Preferred agent: ${suggestion.preferredAgent}`); - if (options.auto) { + if (config.delegationMode === 'auto' || options.auto) { autoDelegate(cwd); console.log('Handoff created automatically.'); } else { diff --git a/src/core/services/decisionService.ts b/src/core/services/decisionService.ts index fb6cf7b..633f6bf 100644 --- a/src/core/services/decisionService.ts +++ b/src/core/services/decisionService.ts @@ -46,6 +46,7 @@ export function listDecisions(cwd: string): ReturnType { } export function getDecision(cwd: string, id: string): { decision: Decision; body: string; filePath: string } { + if (!id) throw new Error('Decision ID is required'); const filePath = join(getEntityDir(cwd, 'decisions'), `${id}.md`); const { frontmatter, body } = readEntity(filePath); return { decision: DecisionSchema.parse(frontmatter), body, filePath }; diff --git a/src/core/services/handoffService.ts b/src/core/services/handoffService.ts index 7880527..1c8a641 100644 --- a/src/core/services/handoffService.ts +++ b/src/core/services/handoffService.ts @@ -48,6 +48,7 @@ export function listHandoffs(cwd: string): ReturnType { } export function getHandoff(cwd: string, id: string): { handoff: Handoff; body: string; filePath: string } { + if (!id) throw new Error('Handoff ID is required'); const filePath = join(getEntityDir(cwd, 'handoffs'), `${id}.md`); const { frontmatter, body } = readEntity(filePath); return { handoff: HandoffSchema.parse(frontmatter), body, filePath }; diff --git a/src/core/services/memoryService.ts b/src/core/services/memoryService.ts index f1225fe..b5952b1 100644 --- a/src/core/services/memoryService.ts +++ b/src/core/services/memoryService.ts @@ -1,12 +1,13 @@ import { join } from 'path'; import { getEntityDir } from '../paths.js'; +import { getNextId } from '../counter.js'; import { writeEntity } from '../files.js'; import { MemorySchema, type Memory } from '../schema.js'; import { Index } from '../index.js'; export function addMemory(cwd: string, options: Partial = {}): Memory { const now = new Date().toISOString(); - const id = `MEM-${Date.now()}`; + const id = getNextId(cwd, 'memory'); const memory: Memory = MemorySchema.parse({ id, title: options.title ?? 'Memory', diff --git a/src/core/services/taskService.ts b/src/core/services/taskService.ts index e48138e..7be8f8d 100644 --- a/src/core/services/taskService.ts +++ b/src/core/services/taskService.ts @@ -37,6 +37,7 @@ export function listTasks(cwd: string, filters?: { status?: string; role?: strin } export function getTask(cwd: string, id: string): { task: Task; body: string; filePath: string } { + if (!id) throw new Error('Task ID is required'); const filePath = join(getEntityDir(cwd, 'tasks'), `${id}.md`); const { frontmatter, body } = readEntity(filePath); return { task: TaskSchema.parse(frontmatter), body, filePath }; diff --git a/src/core/status.ts b/src/core/status.ts index 3d99393..4750884 100644 --- a/src/core/status.ts +++ b/src/core/status.ts @@ -1,7 +1,7 @@ -import { readFileSync, writeFileSync, mkdirSync, existsSync, readdirSync } from 'fs'; +import { writeFileSync, mkdirSync, existsSync } from 'fs'; import { join } from 'path'; import { getEntityDir, getStatusPath } from './paths.js'; -import { readEntity, writeEntity } from './files.js'; +import { readEntity, writeEntity, listEntities } from './files.js'; import { StatusSchema } from './schema.js'; export function generateStatus(cwd: string) { @@ -13,7 +13,7 @@ export function generateStatus(cwd: string) { const blockedTasks: string[] = []; if (existsSync(taskDir)) { - for (const file of listMdFiles(taskDir)) { + for (const file of listEntities(taskDir)) { const { frontmatter } = readEntity(file); if (frontmatter.status === 'open' || frontmatter.status === 'in_progress') { activeTasks.push(String(frontmatter.id)); @@ -42,15 +42,8 @@ export function generateStatus(cwd: string) { return status; } -function listMdFiles(dir: string): string[] { - if (!existsSync(dir)) return []; - return readdirSync(dir) - .filter((f) => f.endsWith('.md')) - .map((f) => join(dir, f)); -} - function recentIds(dir: string, limit: number): string[] { - return listMdFiles(dir) + return listEntities(dir) .map((f) => ({ file: f, ...readEntity(f) })) .sort((a, b) => String(b.frontmatter.createdAt).localeCompare(String(a.frontmatter.createdAt))) .slice(0, limit)