Instruct にworktreeの変更文脈を自動追加

mainからの差分とコミット履歴を指示に含めることで、
エージェントがworktreeの現在の状態を把握できるようにする。
This commit is contained in:
nrslib 2026-01-29 02:11:07 +09:00
parent fc427b4381
commit 8463eb1033

View File

@ -232,6 +232,49 @@ async function selectWorkflowForInstruction(projectDir: string): Promise<string
return await selectOption('Select workflow:', options); return await selectOption('Select workflow:', options);
} }
/**
* Get worktree context: diff stat and commit log from main branch.
*/
function getWorktreeContext(projectDir: string, branch: string): string {
const defaultBranch = detectDefaultBranch(projectDir);
const lines: string[] = [];
// Get diff stat
try {
const diffStat = execFileSync(
'git', ['diff', '--stat', `${defaultBranch}...${branch}`],
{ cwd: projectDir, encoding: 'utf-8', stdio: 'pipe' }
).trim();
if (diffStat) {
lines.push('## 現在の変更内容mainからの差分');
lines.push('```');
lines.push(diffStat);
lines.push('```');
}
} catch {
// Ignore errors
}
// Get commit log
try {
const commitLog = execFileSync(
'git', ['log', '--oneline', `${defaultBranch}..${branch}`],
{ cwd: projectDir, encoding: 'utf-8', stdio: 'pipe' }
).trim();
if (commitLog) {
lines.push('');
lines.push('## コミット履歴');
lines.push('```');
lines.push(commitLog);
lines.push('```');
}
} catch {
// Ignore errors
}
return lines.length > 0 ? lines.join('\n') + '\n\n' : '';
}
/** /**
* Instruct worktree: give additional instructions to modify the worktree. * Instruct worktree: give additional instructions to modify the worktree.
* Executes a task on the worktree and auto-commits if successful. * Executes a task on the worktree and auto-commits if successful.
@ -260,10 +303,16 @@ export async function instructWorktree(
log.info('Instructing worktree', { branch, worktreePath, workflow: selectedWorkflow }); log.info('Instructing worktree', { branch, worktreePath, workflow: selectedWorkflow });
info(`Running instruction on ${branch}...`); info(`Running instruction on ${branch}...`);
// 3. Execute task on worktree // 3. Build instruction with worktree context
const taskSuccess = await executeTask(instruction, worktreePath, selectedWorkflow, projectDir); const worktreeContext = getWorktreeContext(projectDir, branch);
const fullInstruction = worktreeContext
? `${worktreeContext}## 追加指示\n${instruction}`
: instruction;
// 4. Auto-commit if successful // 4. Execute task on worktree
const taskSuccess = await executeTask(fullInstruction, worktreePath, selectedWorkflow, projectDir);
// 5. Auto-commit if successful
if (taskSuccess) { if (taskSuccess) {
const commitResult = autoCommitWorktree(worktreePath, item.taskSlug); const commitResult = autoCommitWorktree(worktreePath, item.taskSlug);
if (commitResult.success && commitResult.commitHash) { if (commitResult.success && commitResult.commitHash) {