fix: Add scope awareness to chat prompt so agents focus on target entities
Pass targetId to buildChatPrompt and add <scope> block that clearly distinguishes primary target files from context files. Context entities may be modified when necessary (e.g. dependency links) but the agent is instructed to focus changes on the primary target.
This commit is contained in:
@@ -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(
|
||||
? `<chat_history>\n${chatHistory.map(m => `[${m.role}]: ${m.content}`).join('\n\n')}\n</chat_history>`
|
||||
: '';
|
||||
|
||||
const scopeBlock = targetType === 'phase'
|
||||
? `<scope>
|
||||
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.
|
||||
</scope>`
|
||||
: `<scope>
|
||||
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.
|
||||
</scope>`;
|
||||
|
||||
return `<role>
|
||||
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.
|
||||
</role>
|
||||
${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
|
||||
</rules>`;
|
||||
}
|
||||
|
||||
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user