diff --git a/apps/server/agent/prompts/chat.ts b/apps/server/agent/prompts/chat.ts index 9e2af87..49158c2 100644 --- a/apps/server/agent/prompts/chat.ts +++ b/apps/server/agent/prompts/chat.ts @@ -11,6 +11,7 @@ export interface ChatHistoryEntry { export function buildChatPrompt( targetType: 'phase' | 'task', + targetId: string, chatHistory: ChatHistoryEntry[], userInstruction: string, ): string { @@ -18,10 +19,46 @@ export function buildChatPrompt( ? `\n${chatHistory.map(m => `[${m.role}]: ${m.content}`).join('\n\n')}\n` : ''; + const scopeBlock = targetType === 'phase' + ? ` +Your **primary target** is the phase described in \`.cw/input/phase.md\` (ID: ${targetId}). + +**What you may modify:** +- The target phase itself (update its name, description, dependencies) +- Tasks that belong to the target phase — these are in \`.cw/input/context/tasks/\` with \`phaseId: ${targetId}\` in their frontmatter. You may create, update, delete, reorder, or restructure these tasks. +- Create new tasks under the target phase (set \`phaseId: ${targetId}\` in frontmatter) + +**Context (modify only if necessary, e.g. to fix dependencies or references):** +- \`.cw/input/initiative.md\` — the parent initiative. Prefer read-only. +- \`.cw/input/context/phases/\` — other phases in the initiative. Prefer read-only — only modify if dependencies or references require it. +- Tasks in \`.cw/input/context/tasks/\` where \`phaseId\` is NOT \`${targetId}\` — these belong to other phases. Only modify if dependencies or references require it. +- \`.cw/input/pages/\` — initiative pages. Prefer read-only. + +Always focus changes on the primary target and its tasks. Only touch context entities when strictly necessary (e.g. updating a dependency link). If unsure, ask the user. +` + : ` +Your **primary target** is the task described in \`.cw/input/task.md\` (ID: ${targetId}). + +**What you may modify:** +- The target task itself (update its name, description, category, priority, dependencies) +- Child tasks (subtasks) of the target task — these are in \`.cw/input/context/tasks/\` with \`parentTaskId: ${targetId}\` in their frontmatter. You may create, update, delete, or restructure these subtasks. +- Create new child tasks under the target task (set \`parentTaskId: ${targetId}\` in frontmatter) + +**Context (modify only if necessary, e.g. to fix dependencies or references):** +- \`.cw/input/initiative.md\` — the parent initiative. Prefer read-only. +- \`.cw/input/phase.md\` — the phase this task belongs to (if any). Prefer read-only. +- \`.cw/input/context/phases/\` — all phases. Prefer read-only. +- Tasks in \`.cw/input/context/tasks/\` where \`parentTaskId\` is NOT \`${targetId}\` and \`id\` is NOT \`${targetId}\` — these are sibling or unrelated tasks. Only modify if dependencies or references require it. +- \`.cw/input/pages/\` — initiative pages. Prefer read-only. + +Always focus changes on the primary target and its child tasks. Only touch context entities when strictly necessary (e.g. updating a dependency link). If unsure, ask the user. +`; + return ` You are an Architect agent in chat mode. You iteratively refine ${targetType} structure and content through conversation with the user. You do NOT write code. ${INPUT_FILES} +${scopeBlock} ${ID_GENERATION} ${SIGNAL_FORMAT} @@ -67,5 +104,6 @@ After writing output files, write \`.cw/output/SUMMARY.md\` with a brief descrip - Apply the minimal set of changes needed for the user's instruction - Preserve existing entity IDs — only use \`cw id\` for new entities - When updating, only include changed fields in frontmatter (plus required \`action\` and \`title\`) +- Focus on the primary target — only modify context entities when strictly necessary `; } diff --git a/apps/server/trpc/routers/chat-session.ts b/apps/server/trpc/routers/chat-session.ts index 2defcdb..6f3fb5e 100644 --- a/apps/server/trpc/routers/chat-session.ts +++ b/apps/server/trpc/routers/chat-session.ts @@ -96,7 +96,7 @@ export function chatSessionProcedures(publicProcedure: ProcedureBuilder) { input.initiativeId, ); - const prompt = buildChatPrompt(input.targetType, chatHistory, input.message); + const prompt = buildChatPrompt(input.targetType, input.targetId, chatHistory, input.message); // Create a task for the chat agent const targetName = input.targetType === 'phase'