diff --git a/packages/memory/src/agents/architect.ts b/packages/memory/src/agents/architect.ts index 7eaf193b..e9fba1a5 100644 --- a/packages/memory/src/agents/architect.ts +++ b/packages/memory/src/agents/architect.ts @@ -28,7 +28,7 @@ Before planning, use memory-read to check for relevant conventions, decisions, a 1. **Research** — Read relevant files, search the codebase, check memory for conventions and decisions 2. **Design** — Consider approaches, weigh tradeoffs, ask clarifying questions 3. **Plan** — Present a clear, detailed plan to the user for review -4. **Execute** — When the user approves, call memory-plan-execute with the full plan +4. **Execute** — When the user approves, save planning state then call memory-plan-execute with the full plan ## Plan Format @@ -41,14 +41,22 @@ Present plans with: ## After Approval -When the user approves, call memory-plan-execute. The plan argument must be **fully self-contained** — the Code agent receiving it has no access to this conversation. Include: +When the user approves the plan: -- Every file path to create or modify -- Specific implementation details (function signatures, data structures, patterns to follow) -- Relevant code snippets or patterns from the existing codebase that the implementation should match -- Dependencies between phases (what must be done before what) -- How to verify each phase works (test commands, expected behavior) -- Any gotchas or constraints discovered during research +1. First, call memory-planning-update to save the planning state: + - objective: Short description of what we're building + - current: "Plan approved, sending to Code agent" + - phases: Each phase from the plan with status "pending" + - findings: Key architectural decisions discovered during research + +2. Then, call memory-plan-execute with the full plan. The plan argument must be **fully self-contained** — the Code agent receiving it has no access to this conversation. Include: + + - Every file path to create or modify + - Specific implementation details (function signatures, data structures, patterns to follow) + - Relevant code snippets or patterns from the existing codebase that the implementation should match + - Dependencies between phases (what must be done before what) + - How to verify each phase works (test commands, expected behavior) + - Any gotchas or constraints discovered during research Do NOT summarize or abbreviate. The plan is the only context the Code agent will have. diff --git a/packages/memory/src/agents/memory.ts b/packages/memory/src/agents/memory.ts index 6da0572d..55bd50d7 100644 --- a/packages/memory/src/agents/memory.ts +++ b/packages/memory/src/agents/memory.ts @@ -192,7 +192,7 @@ You are NOT needed for: - id: The memory ID to delete 4. **memory-planning-update**: Update session planning state - - sessionID: The session to update + - sessionID: Session to update (optional, defaults to current session) - objective: Main task/goal (optional) - current: Current phase or activity (optional) - next: What comes next (optional) @@ -202,7 +202,10 @@ You are NOT needed for: Uses merge semantics - only updates fields provided. 5. **memory-planning-get**: Get planning state for a session - - sessionID: The session to retrieve planning for + - sessionID: Session to retrieve (optional, defaults to current session) + +6. **memory-planning-search**: Search planning states across all sessions in the project + - query: Optional search keyword to filter planning states. Omit to list all. ## Planning State Management diff --git a/packages/memory/src/config.ts b/packages/memory/src/config.ts index 4acdf9fd..fcc797a8 100644 --- a/packages/memory/src/config.ts +++ b/packages/memory/src/config.ts @@ -8,6 +8,7 @@ const ENHANCED_BUILTIN_AGENTS: Record } 'memory-read': true, 'memory-planning-update': true, 'memory-planning-get': true, + 'memory-planning-search': true, }, }, } diff --git a/packages/memory/src/index.ts b/packages/memory/src/index.ts index d24b5d12..dc0b354c 100644 --- a/packages/memory/src/index.ts +++ b/packages/memory/src/index.ts @@ -477,7 +477,7 @@ export function createMemoryPlugin(config: PluginConfig): Plugin { 'memory-planning-update': tool({ description: 'Update the session planning state (phases, objectives, progress). Merge new fields with existing state.', args: { - sessionID: z.string().describe('The session ID to update'), + sessionID: z.string().optional().describe('Session ID to update. Defaults to current session if omitted.'), objective: z.string().optional().describe('The main task/goal'), current: z.string().optional().describe('Current phase or activity'), next: z.string().optional().describe('What comes next'), @@ -489,12 +489,12 @@ export function createMemoryPlugin(config: PluginConfig): Plugin { findings: z.array(z.string()).optional().describe('Key discoveries'), errors: z.array(z.string()).optional().describe('Errors to avoid'), }, - execute: async (args) => { + execute: async (args, context) => { await initPromise - const sessionId = args.sessionID + const sessionId = args.sessionID ?? context.sessionID logger.log(`memory-planning-update: session=${sessionId}`) - const existing = sessionStateService.getPlanningState(sessionId) + const existing = sessionStateService.getPlanningState(sessionId, projectId) const merged: typeof existing = { ...(existing ?? {}), ...(args.objective !== undefined && { objective: args.objective }), @@ -526,14 +526,14 @@ export function createMemoryPlugin(config: PluginConfig): Plugin { 'memory-planning-get': tool({ description: 'Get the current planning state for a session', args: { - sessionID: z.string().describe('The session ID to retrieve planning state for'), + sessionID: z.string().optional().describe('Session ID to retrieve. Defaults to current session if omitted.'), }, - execute: async (args) => { + execute: async (args, context) => { await initPromise - const sessionId = args.sessionID + const sessionId = args.sessionID ?? context.sessionID logger.log(`memory-planning-get: session=${sessionId}`) - const planningState = sessionStateService.getPlanningState(sessionId) + const planningState = sessionStateService.getPlanningState(sessionId, projectId) if (!planningState) { return 'No planning state found for this session' } @@ -569,13 +569,47 @@ export function createMemoryPlugin(config: PluginConfig): Plugin { return sections.join('\n') || 'Planning state exists but is empty' }, }), + 'memory-planning-search': tool({ + description: 'Search planning states across all sessions in the current project. Use this to find planning context from prior sessions.', + args: { + query: z.string().optional().describe('Search keyword to filter planning states. Omit to list all.'), + }, + execute: async (args) => { + await initPromise + logger.log(`memory-planning-search: query="${args.query ?? 'all'}"`) + + const results = args.query + ? sessionStateService.searchPlanningStates(projectId, args.query) + : sessionStateService.listPlanningStates(projectId) + + if (results.length === 0) { + return args.query + ? `No planning states found matching "${args.query}"` + : 'No planning states found for this project' + } + + const formatted = results.map(({ sessionId, planningState, updatedAt }) => { + const parts: string[] = [`**Session:** ${sessionId} (updated ${new Date(updatedAt).toISOString().split('T')[0]})`] + if (planningState.objective) parts.push(` Objective: ${planningState.objective}`) + if (planningState.current) parts.push(` Current: ${planningState.current}`) + if (planningState.next) parts.push(` Next: ${planningState.next}`) + if (planningState.phases && planningState.phases.length > 0) { + const completed = planningState.phases.filter(p => p.status === 'completed').length + parts.push(` Phases: ${completed}/${planningState.phases.length} completed`) + } + return parts.join('\n') + }) + + return `Found ${results.length} planning state(s):\n\n${formatted.join('\n\n')}` + }, + }), 'memory-plan-execute': tool({ description: 'Create a new Code session and send the plan as the first prompt. Call this after the user approves the plan.', args: { plan: z.string().describe('The full implementation plan to send to the Code agent'), title: z.string().describe('Short title for the session (shown in session list)'), }, - execute: async (args) => { + execute: async (args, context) => { await initPromise logger.log(`memory-plan-execute: creating session titled "${args.title}"`) @@ -593,10 +627,12 @@ export function createMemoryPlugin(config: PluginConfig): Plugin { const newSessionId = createResult.data.id logger.log(`memory-plan-execute: created session=${newSessionId}`) + const planningInstruction = `\n\n---\n\nWhen you complete each phase, update the planning state using memory-planning-update with sessionID "${context.sessionID}". Update phase statuses as you progress (pending → in_progress → completed). Set current to describe what you're working on. When all work is done, set current to "Completed".` + const promptResult = await client.session.promptAsync({ path: { id: newSessionId }, body: { - parts: [{ type: 'text' as const, text: args.plan }], + parts: [{ type: 'text' as const, text: args.plan + planningInstruction }], agent: 'Code', }, }) @@ -607,6 +643,16 @@ export function createMemoryPlugin(config: PluginConfig): Plugin { } logger.log(`memory-plan-execute: prompted session=${newSessionId}`) + + const existingState = sessionStateService.getPlanningState(context.sessionID, projectId) + if (existingState) { + sessionStateService.setPlanningState(context.sessionID, projectId, { + ...existingState, + current: `Plan sent to execution session ${newSessionId}`, + }) + logger.log(`memory-plan-execute: updated planning state for source session=${context.sessionID}`) + } + return `Implementation session created and plan sent.\n\nSession: ${newSessionId}\nTitle: ${sessionTitle}\n\nSwitch to this session to begin. You can change the model from the session dropdown.` }, }),