Resilient Implementation
Overview
This workflow demonstrates the power of resilient AI workflows - where implementation automatically continues until completion, no matter what. When an AI agent hits context limits or stops mid-task, this pattern automatically picks up where it left off and keeps going.
The power: Turn unreliable single-shot implementations into bulletproof automated processes. Large specs that would fail 90% of the time now complete 100% of the time, automatically.
Key Features
- Automatic retry - Retries implementation up to 10 times if incomplete
- Context limit handling - Resumes from last checked task if agent stops
- Workspace lifecycle - Automatic setup and cleanup via system phases
- Type-safe responses - JSON-typed responses from slash commands
Example Code
import {
buildSlashCommand,
defineWorkflow,
type CmdImplementSpecResponse,
type CmdReviewSpecImplementationResponse,
} from "agentcmd-workflows";
/**
* Recursive workflow that implements and reviews a spec with automatic retry.
*
* Features:
* - Recursive implementation: retries if agent stops before completion
* - Automatic workspace lifecycle via _system_setup and _system_finalize
* - Spec file resolution happens automatically in system setup phase
*
* Flow:
* 1. Implement phase: calls /cmd:implement-spec up to 10 times until success
* 2. Review phase: calls /cmd:review-spec-implementation once
*/
export default defineWorkflow(
{
id: "implement-review-recurisive-workflow",
name: "Implement Review Recurisive Workflow",
description:
"Implements a spec file and reviews the implementation recursively",
phases: [
{ id: "setup", label: "Setup" },
{ id: "implement", label: "Implement" },
{ id: "review", label: "Review" },
],
},
async ({ event, step }) => {
const { workingDir, specFile } = event.data;
/**
* Implements spec with retry until success (up to 10 attempts).
* Resumes from last checked task if agent stops mid-implementation.
*
* Calls: /cmd:implement-spec (see .claude/commands/cmd/implement-spec.md)
*/
async function implementUntilComplete() {
const MAX_ITERATIONS = 10;
let lastResponse: CmdImplementSpecResponse | undefined;
for (let i = 1; i <= MAX_ITERATIONS; i++) {
const result = await step.agent<CmdImplementSpecResponse>(
`implement-spec-${i}`,
{
agent: "claude",
json: true,
prompt: buildSlashCommand("/cmd:implement-spec", {
specIdOrNameOrPath: specFile,
format: "json",
}),
workingDir,
}
);
lastResponse = result.data;
if (result.data.success) {
return result.data;
}
}
return lastResponse;
}
await step.phase("implement", implementUntilComplete);
await step.phase("review", async () => {
const response = await step.agent<CmdReviewSpecImplementationResponse>(
"review-spec-implementation",
{
agent: "claude",
json: true,
prompt: buildSlashCommand("/cmd:review-spec-implementation", {
specIdOrNameOrPath: specFile,
format: "json",
}),
workingDir,
}
);
return response;
});
}
);How It Works
1. Recursive Implementation Loop
The implementUntilComplete() function retries the implementation phase up to 10 times:
for (let i = 1; i <= MAX_ITERATIONS; i++) {
const result = await step.agent<CmdImplementSpecResponse>(
`implement-spec-${i}`,
{ /* ... */ }
);
if (result.data.success) {
return result.data; // Exit on success
}
}Each retry:
- Gets a unique step ID (
implement-spec-1,implement-spec-2, etc.) - Resumes from the last checked task in the spec
- Continues until
success: trueor max iterations reached
2. Context Limit Handling
When Claude Code reaches context limits mid-implementation:
- The agent stops and returns
success: false - The loop automatically retries with a fresh context
/cmd:implement-specresumes from the last unchecked task- Implementation continues seamlessly
The /cmd:implement-spec command tracks progress via checkboxes in the spec file. Each retry picks up where the previous attempt left off.
3. Review Phase
After successful implementation (or max retries), the review phase runs once:
await step.phase("review", async () => {
const response = await step.agent<CmdReviewSpecImplementationResponse>(
"review-spec-implementation",
{ /* ... */ }
);
return response;
});4. Step Naming Convention
Each retry gets a unique step ID using iteration number:
implement-spec-1- First attemptimplement-spec-2- Second attemptimplement-spec-N- Nth attempt
This ensures:
- Each step is tracked independently in Inngest
- You can see retry history in the workflow dashboard
- No step ID conflicts occur
When to Use This Pattern
Use recursive implementation when:
- Large specs - Implementation may hit context limits
- Complex features - Many files/changes needed
- Reliability needed - Must complete regardless of interruptions
Don't use when:
- Spec is small and fits in one context window
- You want to manually review between attempts
Benefits
- Resilient - Handles interruptions and context limits automatically
- No manual intervention - Retries happen automatically
- Progress tracking - Each attempt is logged and trackable
- Type-safe - Full TypeScript support for responses
Comparison with Basic Pattern
| Feature | Basic | Recursive |
|---|---|---|
| Retries | No | Yes (up to 10x) |
| Context limits | Fails | Handles automatically |
| Progress resume | No | Yes |
| Complexity | Simple | Moderate |
Next Steps
- Self-Correcting Workflows - Add review-based retry cycles
- Context Sharing - Share data between phases
- Type-Safe Slash Commands - Build reusable workflow commands