Skip to content

Commit da0b564

Browse files
committed
fix(copilot): auto-cleanup worktree after successful --apply
When --apply successfully applies changes from the worktree to the main project, automatically clean up the worktree with force since the changes have already been extracted. Also updated hint messages to suggest using --force with cleanup command.
1 parent 105c52b commit da0b564

1 file changed

Lines changed: 30 additions & 43 deletions

File tree

src/klondike_spec_cli/copilot.py

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -158,81 +158,60 @@ def build_prompt(
158158
instructions: str | None = None,
159159
worktree_info: WorktreeInfo | None = None,
160160
) -> str:
161-
"""Build the prompt for Copilot.
161+
"""Build a brief prompt for Copilot.
162+
163+
Behavioral rules and project context come from .github/copilot-instructions.md
164+
which Copilot CLI auto-loads. This function only needs to specify the task.
162165
163166
Args:
164167
focus_feature: Optional feature to focus on
165168
instructions: Additional instructions
166169
worktree_info: Worktree information if running in worktree
167170
168171
Returns:
169-
The prompt string
172+
The prompt string (brief task description)
170173
"""
171174
prompt_parts: list[str] = []
172175

173176
if worktree_info:
174-
# ULTRA-DIRECT PROMPT for worktree mode
175-
# Previous attempts: Copilot was doing "startup routine" instead of implementing
176-
# This version: Feature implementation is THE ONLY GOAL
177+
# Worktree mode - Copilot reads .github/copilot-instructions.md for rules
178+
# We only need to specify WHAT to do, not HOW to behave
177179

178180
if focus_feature:
179-
# Put the feature FIRST, make it the only thing that matters
180-
prompt_parts.append(f"# IMPLEMENT: {focus_feature.id} - {focus_feature.description}")
181-
prompt_parts.append("")
182-
prompt_parts.append("Your ONLY task is to implement this feature. Nothing else.")
181+
# Feature implementation task
182+
prompt_parts.append(f"Implement {focus_feature.id}: {focus_feature.description}")
183183
prompt_parts.append("")
184184
if focus_feature.acceptance_criteria:
185-
prompt_parts.append("## Requirements (implement ALL):")
185+
prompt_parts.append("Acceptance criteria:")
186186
for ac in focus_feature.acceptance_criteria:
187187
prompt_parts.append(f"- {ac}")
188188
prompt_parts.append("")
189-
prompt_parts.append("## Instructions")
190-
prompt_parts.append("1. Read the codebase to understand the project structure")
191-
prompt_parts.append("2. Write the code to implement this feature")
192189
prompt_parts.append(
193-
"3. Commit when done: git add -A && git commit -m 'feat(F0XX): description'"
190+
"You are in an isolated git worktree. Implement the feature and commit when done."
194191
)
195-
prompt_parts.append("")
196-
prompt_parts.append("You are in an isolated git worktree - safe to make changes.")
197-
prompt_parts.append("")
198-
prompt_parts.append("## What NOT to do")
199-
prompt_parts.append(
200-
"- Do NOT run `klondike status`, `klondike validate`, or `klondike session start`"
201-
)
202-
prompt_parts.append("- Do NOT do a 'startup routine' or 'orientation'")
203-
prompt_parts.append("- Do NOT ask for permission - just implement")
204-
prompt_parts.append("")
205-
prompt_parts.append("## What you CAN do")
206-
prompt_parts.append("- Read files, run the dev server, run tests/lints")
207-
prompt_parts.append(
208-
"- Use `klondike feature show` if you need more context on a feature"
209-
)
210-
prompt_parts.append("- Make commits as you work")
211192
else:
212-
# No feature specified - general worktree session
213-
prompt_parts.append("# Worktree Session")
214-
prompt_parts.append("")
215-
prompt_parts.append("You are in an isolated git worktree.")
193+
# No feature specified - let Copilot pick based on klondike status
216194
prompt_parts.append(
217-
"Run `klondike status` to see available features, then implement one."
195+
"Run `klondike status` to see the project state and pick a feature to implement."
218196
)
197+
prompt_parts.append("You are in an isolated git worktree.")
219198
else:
220-
# Normal mode - use template
199+
# Normal mode - use template for session startup
221200
template_content = _load_prompt_template("session-start.prompt.md")
222201
prompt_parts.append(template_content)
223202

224-
# Add feature implementation instruction if specified
203+
# Add feature focus if specified
225204
if focus_feature:
226205
prompt_parts.append("")
227206
prompt_parts.append(
228-
f"After completing the session start routine, implement feature {focus_feature.id}: {focus_feature.description}"
207+
f"Focus on implementing {focus_feature.id}: {focus_feature.description}"
229208
)
230209
if focus_feature.acceptance_criteria:
231210
prompt_parts.append("Acceptance criteria:")
232211
for ac in focus_feature.acceptance_criteria:
233-
prompt_parts.append(f" - {ac}")
212+
prompt_parts.append(f"- {ac}")
234213

235-
# Additional instructions
214+
# Additional instructions from CLI
236215
if instructions:
237216
prompt_parts.append("")
238217
prompt_parts.append(instructions)
@@ -460,10 +439,18 @@ def _handle_worktree_cleanup(worktree_info: WorktreeInfo, config: CopilotConfig)
460439
try:
461440
apply_worktree_changes(worktree_info)
462441
echo(" ✅ Changes applied successfully")
442+
443+
# Auto-cleanup after successful apply (changes are now in main project)
444+
echo(" 🧹 Cleaning up worktree...")
445+
try:
446+
cleanup_worktree(worktree_info, force=True)
447+
echo(" ✅ Worktree removed")
448+
except WorktreeError as e:
449+
echo(f" ⚠️ Failed to cleanup: {e}")
450+
echo(" 💡 Run: klondike copilot cleanup --force")
463451
except WorktreeError as e:
464452
raise PithException(f"Failed to apply changes: {e}") from e
465-
466-
if config.cleanup_after:
453+
elif config.cleanup_after:
467454
echo(" 🧹 Cleaning up worktree...")
468455
try:
469456
cleanup_worktree(worktree_info, force=True)
@@ -474,7 +461,7 @@ def _handle_worktree_cleanup(worktree_info: WorktreeInfo, config: CopilotConfig)
474461
echo("")
475462
echo("💡 Worktree preserved. To manage:")
476463
echo(f" cd {worktree_info.worktree_path}")
477-
echo(" # Or cleanup with: klondike copilot cleanup")
464+
echo(" # Or cleanup with: klondike copilot cleanup --force")
478465
else:
479466
echo(" ℹ️ No changes detected")
480467

0 commit comments

Comments
 (0)