feat: add subagent resume capability

Add ability to resume interrupted subagent executions using Task tool's
resume parameter. Tracks agent IDs during execution and enables seamless
continuation after session timeout, rate limits, or crashes.

- New /gsd:resume-task command for resuming interrupted agents
- Agent ID tracking in execute-phase workflow
- Dual storage: current-agent-id.txt (fast) + agent-history.json (audit)
- Auto-detection of interrupted agents in resume-project workflow
- File conflict warnings before resume

Closes #37

Co-Authored-By: davesienkowski <davesienkowski@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Lex Christopherson
2026-01-10 14:19:01 -06:00
parent 992d034df2
commit 6ba850712f
5 changed files with 647 additions and 5 deletions

View File

@@ -204,15 +204,48 @@ No segmentation benefit - execute entirely in main
**For fully autonomous plans:**
```
Use Task tool with subagent_type="general-purpose":
1. Run init_agent_tracking step first (see step below)
Prompt: "Execute plan at .planning/phases/{phase}-{plan}-PLAN.md
2. Use Task tool with subagent_type="general-purpose":
This is an autonomous plan (no checkpoints). Execute all tasks, create SUMMARY.md in phase directory, commit with message following plan's commit guidance.
Prompt: "Execute plan at .planning/phases/{phase}-{plan}-PLAN.md
Follow all deviation rules and authentication gate protocols from the plan.
This is an autonomous plan (no checkpoints). Execute all tasks, create SUMMARY.md in phase directory, commit with message following plan's commit guidance.
When complete, report: plan name, tasks completed, SUMMARY path, commit hash."
Follow all deviation rules and authentication gate protocols from the plan.
When complete, report: plan name, tasks completed, SUMMARY path, commit hash."
3. After Task tool returns with agent_id:
a. Write agent_id to current-agent-id.txt:
echo "[agent_id]" > .planning/current-agent-id.txt
b. Append spawn entry to agent-history.json:
{
"agent_id": "[agent_id from Task response]",
"task_description": "Execute full plan {phase}-{plan} (autonomous)",
"phase": "{phase}",
"plan": "{plan}",
"segment": null,
"timestamp": "[ISO timestamp]",
"status": "spawned",
"completion_timestamp": null
}
4. Wait for subagent to complete
5. After subagent completes successfully:
a. Update agent-history.json entry:
- Find entry with matching agent_id
- Set status: "completed"
- Set completion_timestamp: "[ISO timestamp]"
b. Clear current-agent-id.txt:
rm .planning/current-agent-id.txt
6. Report completion to user
```
**For segmented plans (has verify-only checkpoints):**
@@ -247,6 +280,54 @@ Quality maintained through small scope (2-3 tasks per plan)
See step name="segment_execution" for detailed segment execution loop.
</step>
<step name="init_agent_tracking">
**Initialize agent tracking for subagent resume capability.**
Before spawning any subagents, set up tracking infrastructure:
**1. Create/verify tracking files:**
```bash
# Create agent history file if doesn't exist
if [ ! -f .planning/agent-history.json ]; then
echo '{"version":"1.0","max_entries":50,"entries":[]}' > .planning/agent-history.json
fi
# Clear any stale current-agent-id (from interrupted sessions)
# Will be populated when subagent spawns
rm -f .planning/current-agent-id.txt
```
**2. Check for interrupted agents (resume detection):**
```bash
# Check if current-agent-id.txt exists from previous interrupted session
if [ -f .planning/current-agent-id.txt ]; then
INTERRUPTED_ID=$(cat .planning/current-agent-id.txt)
echo "Found interrupted agent: $INTERRUPTED_ID"
fi
```
**If interrupted agent found:**
- The agent ID file exists from a previous session that didn't complete
- This agent can potentially be resumed using Task tool's `resume` parameter
- Present to user: "Previous session was interrupted. Resume agent [ID] or start fresh?"
- If resume: Use Task tool with `resume` parameter set to the interrupted ID
- If fresh: Clear the file and proceed normally
**3. Prune old entries (housekeeping):**
If agent-history.json has more than `max_entries`:
- Remove oldest entries with status "completed"
- Never remove entries with status "spawned" (may need resume)
- Keep file under size limit for fast reads
**When to run this step:**
- Pattern A (fully autonomous): Before spawning the single subagent
- Pattern B (segmented): Before the segment execution loop
- Pattern C (main context): Skip - no subagents spawned
</step>
<step name="segment_execution">
**Detailed segment execution loop for segmented plans.**
@@ -299,8 +380,36 @@ For Pattern A (fully autonomous) and Pattern C (decision-dependent), skip this s
- Deviations encountered
- Any issues or blockers"
**After Task tool returns with agent_id:**
1. Write agent_id to current-agent-id.txt:
echo "[agent_id]" > .planning/current-agent-id.txt
2. Append spawn entry to agent-history.json:
{
"agent_id": "[agent_id from Task response]",
"task_description": "Execute tasks [X-Y] from plan {phase}-{plan}",
"phase": "{phase}",
"plan": "{plan}",
"segment": [segment_number],
"timestamp": "[ISO timestamp]",
"status": "spawned",
"completion_timestamp": null
}
Wait for subagent to complete
Capture results (files changed, deviations, etc.)
**After subagent completes successfully:**
1. Update agent-history.json entry:
- Find entry with matching agent_id
- Set status: "completed"
- Set completion_timestamp: "[ISO timestamp]"
2. Clear current-agent-id.txt:
rm .planning/current-agent-id.txt
```
C. If routing = Main context:

View File

@@ -69,6 +69,12 @@ for plan in .planning/phases/*/*-PLAN.md; do
summary="${plan/PLAN/SUMMARY}"
[ ! -f "$summary" ] && echo "Incomplete: $plan"
done 2>/dev/null
# Check for interrupted agents
if [ -f .planning/current-agent-id.txt ] && [ -s .planning/current-agent-id.txt ]; then
AGENT_ID=$(cat .planning/current-agent-id.txt | tr -d '\n')
echo "Interrupted agent: $AGENT_ID"
fi
```
**If .continue-here file exists:**
@@ -81,6 +87,12 @@ done 2>/dev/null
- Execution was started but not completed
- Flag: "Found incomplete plan execution"
**If interrupted agent found:**
- Subagent was spawned but session ended before completion
- Read agent-history.json for task details
- Flag: "Found interrupted agent"
</step>
<step name="present_status">
@@ -103,6 +115,14 @@ Present complete project status to user:
⚠️ Incomplete work detected:
- [.continue-here file or incomplete plan]
[If interrupted agent found:]
⚠️ Interrupted agent detected:
Agent ID: [id]
Task: [task description from agent-history.json]
Interrupted: [timestamp]
Resume with: /gsd:resume-task
[If deferred issues exist:]
📋 [N] deferred issues awaiting attention
@@ -120,6 +140,10 @@ Present complete project status to user:
<step name="determine_next_action">
Based on project state, determine the most logical next action:
**If interrupted agent exists:**
→ Primary: Resume interrupted agent (/gsd:resume-task)
→ Option: Start fresh (abandon agent work)
**If .continue-here file exists:**
→ Primary: Resume from checkpoint
→ Option: Start fresh on current plan
@@ -154,6 +178,8 @@ Present contextual options based on project state:
What would you like to do?
[Primary action based on state - e.g.:]
1. Resume interrupted agent (/gsd:resume-task) [if interrupted agent found]
OR
1. Resume from checkpoint (/gsd:execute-plan .planning/phases/XX-name/.continue-here-02-01.md)
OR
1. Execute next plan (/gsd:execute-plan .planning/phases/XX-name/02-02-PLAN.md)

View File

@@ -0,0 +1,259 @@
<trigger>
Use this workflow when:
- User runs /gsd:resume-task
- Need to continue interrupted subagent execution
- Resuming work after session ended mid-plan
</trigger>
<purpose>
Resume an interrupted subagent execution using the Task tool's resume parameter.
Enables seamless continuation of autonomous work that was interrupted by session timeout, user exit, or crash.
</purpose>
<process>
<step name="parse_arguments">
Parse the optional agent_id argument:
```bash
# Check if argument provided
if [ -n "$ARGUMENTS" ]; then
AGENT_ID="$ARGUMENTS"
echo "Using provided agent ID: $AGENT_ID"
else
# Read from current-agent-id.txt
if [ -f .planning/current-agent-id.txt ] && [ -s .planning/current-agent-id.txt ]; then
AGENT_ID=$(cat .planning/current-agent-id.txt | tr -d '\n')
echo "Using current agent ID: $AGENT_ID"
else
echo "ERROR: No agent to resume"
exit 1
fi
fi
```
**If no agent ID found:**
Present error:
```
No active agent to resume.
There's no interrupted agent recorded. This could mean:
- No subagent was spawned in the current plan
- The last agent completed successfully
- .planning/current-agent-id.txt was cleared
Use /gsd:progress to check project status.
```
**If agent ID found:** Continue to validate_agent step.
</step>
<step name="validate_agent">
Validate the agent exists and is resumable:
```bash
# Check if agent-history.json exists
if [ ! -f .planning/agent-history.json ]; then
echo "ERROR: No agent history found"
exit 1
fi
# Read history and find the agent
cat .planning/agent-history.json
```
**Parse agent-history.json and find entry matching AGENT_ID:**
Check the entry status:
- **If status = "completed":** Error - agent already completed
- **If status = "spawned" or "interrupted":** Valid for resume
- **If not found:** Error - agent ID not in history
**If agent already completed:**
```
Agent already completed.
Agent ID: [id]
Completed: [completion_timestamp]
Task: [task_description]
This agent finished successfully. No resume needed.
```
**If agent not found:**
```
Agent ID not found in history.
ID: [provided_id]
Available agents:
- [list most recent 5 agents with status]
Did you mean one of these?
```
**If valid for resume:** Continue to check_conflicts step.
</step>
<step name="check_conflicts">
Check for file modifications since the agent was spawned.
**Read the agent entry to get context:**
- phase, plan, segment information
- timestamp of spawn
**Check for git changes since spawn:**
```bash
# Get files modified since agent spawn
# Note: This is a best-effort check - relies on git status
git status --short
```
**If modifications detected:**
Use AskUserQuestion to warn user:
```
Files modified since agent was interrupted:
- [list files]
These changes may conflict with the agent's work.
Options:
1. Continue anyway - Agent will resume with current files
2. Abort - Review changes first
Select option:
```
Wait for user response.
**If user selects "Abort":**
```
Resume aborted. Review your changes and run /gsd:resume-task when ready.
```
End workflow.
**If user selects "Continue anyway" or no conflicts found:**
Continue to update_status step.
</step>
<step name="update_status">
Update the agent status to "interrupted" if it was "spawned" (marking the interruption point):
```bash
# Read current history
HISTORY=$(cat .planning/agent-history.json)
```
Update the entry in agent-history.json:
- If status was "spawned", change to "interrupted"
- Add note about resume attempt
This provides audit trail of the interruption before resume.
</step>
<step name="resume_agent">
Resume the agent using Task tool's resume parameter:
```
Resuming agent: [agent_id]
Task: [task_description from history]
Phase: [phase]-[plan]
The agent will continue from where it left off...
```
**Use Task tool with resume parameter:**
```
Task(
description: "Resume interrupted agent",
prompt: "Continue your previous work. You were executing [task_description].",
subagent_type: "general-purpose",
resume: "[AGENT_ID]"
)
```
Wait for agent completion.
**On agent completion:**
- Capture any results returned
- Continue to completion_update step
</step>
<step name="completion_update">
Update tracking files on successful completion:
**1. Update agent-history.json:**
Add new entry marking the resume completion:
```json
{
"agent_id": "[AGENT_ID]",
"task_description": "[original task] (resumed)",
"phase": "[phase]",
"plan": "[plan]",
"segment": [segment or null],
"timestamp": "[now]",
"status": "completed",
"completion_timestamp": "[now]"
}
```
**2. Clear current-agent-id.txt:**
```bash
# Clear the current agent file
> .planning/current-agent-id.txt
```
**3. Present completion message:**
```
Agent resumed and completed successfully.
Agent ID: [id]
Task: [task_description]
Original spawn: [original_timestamp]
Completed: [now]
The agent's work has been incorporated. Check git status for changes.
```
</step>
<step name="handle_errors">
Error handling for resume failures:
**If Task tool returns error on resume:**
```
Failed to resume agent.
Agent ID: [id]
Error: [error message]
Possible causes:
- Agent context may have expired
- Agent may have been invalidated
Options:
1. Start fresh - Execute plan from beginning
2. Check status - Review what was completed
Run /gsd:execute-plan to start fresh if needed.
```
Do NOT clear current-agent-id.txt on error - allow retry.
</step>
</process>
<success_criteria>
Resume is complete when:
- [ ] Agent resumed successfully via Task tool resume parameter
- [ ] Agent-history.json updated with completion status
- [ ] Current-agent-id.txt cleared
- [ ] User informed of completion
</success_criteria>